Linux – Making a script into a linux service

bashlinuxservice

— 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

Best Answer

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.

Related Topic