3

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

3 Answers3

9

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 the daemon function which has an option for specifying the user to run as.

I'd personally sway towards the latter all other things being equal.

Jason Tan
  • 2,792
3

If you write a redhat-style init script, the daemon function has a --user option.

daemon --user=$runasuser --pidfile="$PIDFILE" $yourbinary $youroptions
Dan Pritts
  • 3,336
  • 29
  • 28
0

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

 echo password | sudo -S -u username command

What are you doing? Maybe there's a better way?

EDIT

Thanks TCampbell

Matt Simmons
  • 20,584