Here's my Init script which I have at my Ubuntu workstation. I need to run a command as another user than root, but I just can't get my head around how it should be done. Neither sudo -u or su newuser seems to work.

The script:

respawn
console none

start on runlevel [2345]
stop on runlevel [06]

script
  su "anotherUser" -c ./myCommand
end script
share|improve this question
    
use this type of command "su -c 'your command' different_user" – vikky Jan 20 '12 at 11:48
    
As @Henrick pointed out in their answer below, upstart scripts can use the setuid configuration stanza to set the user. – clayzermk1 Oct 2 '13 at 20:58
up vote 18 down vote accepted

I use this:

su -l $MUSER -c "myCommand args..."

Update: Since there is interest in this answer, I explain the way I use it here.

We run servers as normal linux users, not root. The username contains three parts:

service, customer, stage

This way we can run several services for several customers in one linux OS.

Example: foo_bar_p Service "foo" of customer "bar" and "p" means production

Here is the part of the init script. The init script can be executed as root or as foo_bar_p user:

# /etc/init.d/foo_bar_p-celeryd
# scriptname contains linux username  
SCRIPT_NAME=`basename "$0"`
SYSTEM=${SCRIPT_NAME%*-celeryd}

U=`id -nu`

if [ ! $U == $SYSTEM ]; then
    if [ $U == "root" ]; then
        # use "-l (login)" to delete the environment variables of the calling shell.
        exec su -l $SYSTEM -c "$0 $@"
    fi
    echo "Script must be run from $SYSTEM or root. You are '$U'"
    rc_exit 1
fi

# OK, now I am foo_bar_p
cd
. $HOME/.bashrc
....
share|improve this answer
    
Fantastic. Thanks! – Industrial Jan 20 '12 at 12:35
    
What does the -l switch do?? – Henrik Mar 30 '13 at 12:25
    
-l | --login A way to make the shell started a login shell. – Henrik Mar 30 '13 at 12:50
    
I didn't get where the actual start of the script happens? The author said: su -l $MUSER -c "myCommand args..." and then he shows the script foo_bar_p-celeryd which checks if it was started by root or user. But how actually is determined (defined) who starts the script foo_bar_p-celeryd? – beemaster May 2 '17 at 21:08
    
@beemaster the script /etc/init.d/foo_bar_p-celeryd is called by the system-v init. This is old unix stuff. Today I use systemd, and I am very happy with systemd. Does this answer you comment? – guettli May 3 '17 at 13:27

For upstart, use:

setuid myuser
exec command args
share|improve this answer
    
Nice! Does it load .bashrc, profile and stuff like that ? – Arthur Corenzan Oct 15 '14 at 13:50

su is probably a more universal approach, but this is also possible on some common distributions with sudo:

sudo -u $MUSER $COMMAND $ARGS

(just reread your question and didn't realize that doesn't work for you, but it has worked for me in init scripts)

share|improve this answer
    
Have tried this in /etc/rc.local but is doesn't work on Ubuntu 16.04. Also tried this: su -l user -c /home/user/script. Any suggestions? – beemaster May 3 '17 at 5:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.