Run command as a standard user instead of root?
-
Hi there,
I have a command that I want to use under the 'pi' user instead of root.
Am I able to run a command as the 'pi' user or install TCMD as 'Pi' instead?
Thanks! -
@Lewis-S, the simple way to run a command as the pi user would be to make a command like this:
su - pi -c 'ls ~'
That command will run ls ~ as the pi user, so it will list the files in the pi user's home directory.
su is "switch user" command. Here's the man page for it: https://man7.org/linux/man-pages/man1/su.1.html
-
@Russ
Hi Russ,
That didn't work. I think systemctl is too big of an ask to run via su - pi.
I might have to make it a system service, if it is not possible to run TCMD as non-root? -
@Lewis-S, I just tested this on my Raspberry Pi and it worked for me. This is my commands.json entry.
{ "trigger": "pi user test", "command": "su - pi -c 'ls ~ > /tmp/ls.txt'", "offCommand": "", "ground": "background", "voice": "", "voiceReply": "", "allowParams": "false" },
It is possible to run the agent as the pi user, but I figured this would be the easier method.
If you still want to try running the agent as pi, you could start running this command while logged in as the pi user:
triggercmdagent --console
It will setup a new .TRIGGERcmdData folder in the pi user's home directory, so it will try to create another computer in your TRIGGERcmd account, so you'll either need to delete your existing computer from your account, or have a subscription.
-
Hi Russ,
I did try that, but got the same error again. I think systemctl is a command too powerful to run via su.
I'll look at other ways, like moving it to a system service or making it the root user's serivce.
Thank you! -
@Lewis-S, are you trying to run systemctl as the pi user via TRIGGERcmd? If not, what command are trying to run, and what error are you getting?
If I know what you're trying to accomplish, I might be able to help.
-
@Russ Yes, that's what I want to do.
I get " Failed to connect to bus: No such file or directory". -
Hi @Russ
Update: I asked elsewhere andsu pi -c 'XDG_RUNTIME_DIR="/run/user/1000" systemctl --user status monitor.service'
works, as it adds environment variables needed for systemctl to run.
Thanks!
-
@Lewis-S, awesome. Nice job figuring that out.