Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to execute a script every time my server start up. The problem is that I need to be a certain user to execute the script, if I try to do it as root it cant find certain packages (such as ruby).

I try to change to xxx user01.

sudo su user01
/etc/init.d/script start

This doesn't work however.

share|improve this question
Post your init script? – quanta Aug 30 '12 at 16:07
Do you have some logs ? – Dom Aug 30 '12 at 16:08
Why don't you simply use su in the rc script? – Mat Aug 30 '12 at 16:08
up vote 16 down vote accepted

Running sudo su user01 in a script does not mean the following commands are sent to the resultant shell. In fact, it likely means a new shell is spawned as user01, which never exits!

Two things:

  • You can execute a command as another user either by passing the -c 'command...' argument to su, like su user01 -c '/etc/init.d/script start'.
  • Starting a service that uses /etc/init.d from rc.local isn't the correct thing to do. You want to use enable the service at startup using your distribution tools, like chkconfig or update-rc.d. You also don't want jobs in /etc/init.d that shouldn't be started as root. The jobs themselves can feel free to fork to another user account, but should be invoked by root.
share|improve this answer

You could put something in /etc/crontab and run it @reboot

@reboot username /etc/init.d/script start
share|improve this answer
this only works on reboots, not startups – MarZab Mar 5 '15 at 11:53

You can just run the command through sudo like this:

sudo -H -u user01 /etc/init.d/script start

-H sets the HOME environment variable to that of the user

-u specifies the username to run as

share|improve this answer

Surely you can't run that script with sudo because of requiretty ( enforces that users must be logged) in sudoers. You may want disable, which is somewhat risky

Also there is a somewhat old tool called runsuid:


share|improve this answer

Your Answer


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.