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
I use this:
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: