-- 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 --
#!/bin/sh
#
# 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)
# Start daemons.
echo -n $"Starting report-listen daemon: "
echo
daemon /home/webreports/report-listen
echo
;;
stop)
# Stop daemons.
echo -n $"Shutting down report-listen: "
killproc /home/webreports/report-listen
echo
# Do clean-up works here like removing pid files from /var/run, etc.
;;
status)
status /home/webreports/report-listen
;;
restart)
$0 stop
$0 start
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
exit 1
esac
exit 0
Use
su
to run the script as a different user: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.
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:
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):
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: