I was wondering how to run a command as another user from a script.
I have the script's owner set as root. I also have the following command being run within the script to run the command as the hudson user:
su -c command hudson
Is this the correct syntax?
Best Answer
Yes. Here's the
--help
:And some testing (I used
sudo
as I don't know the password for thenobody
account)When your command takes arguments you need to quote it. If you don't, strange things will occur. Here I am —as root— trying to create a directory in /home/oli (as oli) without quoting the full command:
It's only read
mkdir
as the value for the-c
flag and it's trying to use/home/oli/java
as the username. If we quote it, it just works: