I want to write an init service script which runs the program as a particular user (and not root). I will then chkconfig this script and install into my production run level.
I could just put a su command in the script but I was wondering if there is a best practise of doing this.
Thanks, Garry
You can just
su
. You won't need the password because the script will initally be running as root.There's also the
runuser
command.If you use
/etc/init.d/functions
you can use thedaemon
function which has an option for specifying the user to run as.I'd personally sway towards the latter all other things being equal.
If you write a redhat-style init script, the
daemon
function has a--user
option.The best practice might be to use sudo, and modify the necessary files to make sure it happens automatically without being prompted for a password. If you don't mind the password being in the script, I think you can do something like
What are you doing? Maybe there's a better way?
EDIT
Thanks TCampbell