5
\$\begingroup\$

This is the first time I've written a start-stop-daemon script, so I would love some feedback. :) The goal is to have it start late, when the network and everything is ready. I also have 2 other start-stop-daemons that depend on this once being started before they launch and are nearly identical.

#! /bin/sh
# chkconfig 345 85 60
# description: startup script for foo
# processname: foo

NAME=foo
DIR=/etc/foo/services
EXEC=foo.py
PID_FILE=/var/run/foo.pid
IEXE=/etc/init.d/foo
RUN_AS=root

### BEGIN INIT INFO
# Provides:          foo
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     5
# Default-Stop:      0 1 2 3 6
# Description:       Starts the foo service
### END INIT INFO

if [ ! -f $DIR/$EXEC ]
then
        echo "$DIR/$EXEC not found."
        exit
fi

case "$1" in
  start)
        echo -n "Starting $NAME"
    cd $DIR
    start-stop-daemon -d $DIR --start --background --pidfile $PID_FILE --make-pidfile --exec $EXEC --quiet
        echo "$NAME are now running."
        ;;
  stop)
    echo -n "Stopping $NAME"
        kill -TERM `cat $PID_FILE`
    rm $PID_FILE
        echo "$NAME."
        ;;
  force-reload|restart)
        $0 stop
        $0 start
        ;;
  *)
        echo "Use: /etc/init.d/$NAME {start|stop|restart|force-reload}"
        exit 1
    ;;
esac
exit 0
\$\endgroup\$
  • \$\begingroup\$ Awesome script. I see you activated the --background flag. In fact, this ties into my interest on this question (codereview.stackexchange.com/q/2841/4307). I plan on making a Daemon too. \$\endgroup\$ – Volomike Jun 7 '11 at 5:05
  • \$\begingroup\$ You might want to look on Ubuntu with the /etc/init.d/skeleton sample script, and then edit that. That way, you get the status command, etc. \$\endgroup\$ – Volomike Jun 7 '11 at 5:30
4
\$\begingroup\$

Probably, you also may implement condrestart:

The condrestart (conditional restart) option only starts the daemon if it is currently running. This option is useful for scripts, because it does not start the daemon if it is not running.

Quote from question about condrestart at LinuxQuestions

| improve this answer | |
\$\endgroup\$

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.