-- EDIT, still some issues --

OK my script uses a loop to wait for network connections. So when I run it, even with daemon it will just sit there and not take me back to the shell. I tried su -c "/home/webreports/report-list &" USER but it tried to run as the user & even though i have it in quotes, i even tried single quotes.

-- Original -- I have made a script (yet to be tested) for running a bash script as a service. I have two questions.

1) how do i get it to run as a specific user? the software we use CANNOT be run as root and will fail horribly if it does (horrible software we are sadly stuck with). So how do i make it run the service as user "JOEBOB" lets say.

2) Do I just put the script file into "/etc/rc5.d " to be able to use "service report-listen start" ?

--- Script --

# myservice     This shell script takes care of starting and stopping
#               the /home/webreports/report-listen

# Source function library
. /etc/rc.d/init.d/functions

# Do preliminary checks here, if any
#### START of preliminary checks #########

##### END of preliminary checks #######

# Handle manual control parameters like start, stop, status, restart, etc.

case "$1" in
    # Start daemons.

    echo -n $"Starting report-listen daemon: "
    daemon /home/webreports/report-listen

    # Stop daemons.
    echo -n $"Shutting down report-listen: "
    killproc /home/webreports/report-listen

    # Do clean-up works here like removing pid files from /var/run, etc.
    status /home/webreports/report-listen

    $0 stop
    $0 start

    echo $"Usage: $0 {start|stop|status|restart}"
    exit 1

exit 0
  • 1
    I think not being able to run as root is a feature :)!
    – Zypher
    Jan 7, 2010 at 18:35
  • It would be except it will let you run as root and then change all the permissions and lock everyone out, then crash. Jan 7, 2010 at 18:37
  • What distribution? Jan 7, 2010 at 18:43
  • Redhat enterprise Jan 7, 2010 at 18:49

5 Answers 5


Use su to run the script as a different user:

daemon su -c /home/webreports/report-listen johndoe

where johndoe is the user you want it to run as.

Put the script in /etc/init.d/myservice, then symlink it to /etc/rc.d/S99myservice.

  • Only issue I have is that it doesn't seem to be running the script in the background. I tried daemon su -c '/home/webreports/report-listen &' USER but then it tried to run as the user & even though I have it in quotes Jan 7, 2010 at 17:24

just copying will not do the job. You have to take care the program is decoupled from stdin and stdout. Therefor all output has to be printed to a logfile. You also have to background the program, which should be done by the daemon function.


If you're using Ubuntu (Debian based OS) you can base your script on /etc/init.d/skeleton and replace DAMEON var:

DAEMON=su -c "/home/webreports/report-listen" joebob

You will have a pid file also for clean start/stop.

You can start it automatically during boot with (Assuming your service script is named /etc/init.d/my-report-listen):

$ sudo update-rc.d my-report-listen defaults

This method is just an attempt, not a recipie that works. I hope it helps


Daemonising code is hard to get right. For this, I use daemontools -- basically it does the hard work of running in the background, and you just write your script as though you're going to run it in the foreground, and then tell daemontools to run it instead.

See http://cr.yp.to/daemontools.html for all the documentatory awesome.


You haven't mentioned what distribution you're using, but it seems like you're using a Red Hat based one. I'll base my answer on that assumption.

To answer the first part of your question, about running the script on startup. You have to put the script in /etc/rc.d/init.d, and add a "chkconfig" compatible line to allow you to add it to the correct levels with the "chkconfig" command. see a previous question on that issue.

As for starting the script with a specific user. Since you're already using the "daemon" function, you can try using the "--user" switch:

daemon --user=reportsuser /home/webreports/report-listen

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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