Bash – Whats the proper what to terminate a script using start-stop-daemon

bashstart-stop-daemon

I am using a start-stop-daemon to make a INIT script for my script. I am using –make-pidfile cause my script doesnt make its own pid. I can start my script using start and pid file generates with appropriate PID. But the stop function doesnt work. As per start-stop-daemon manual,

–stop Checks for the existence of a specified process. If such a
process exists, start-stop-daemon sends it the signal specified
by –signal, and exits with error status 0. If such a process
does not exist, start-stop-daemon exits with error status 1 (0
if –oknodo is specified). If –retry is specified, then
start-stop-daemon will check that the process(es) have terminated.

I didnt find find any documentation for –signal itself. Like how to specify –signal if I want to send a SIGTERM.

case "$1" in
  start|debug)
        log_daemon_msg "Starting $DESC: $NAME"
        start-stop-daemon --start --quiet --background --make-pidfile --pidfile $PIDFILE \
                --exec $DAEMON || log_failure_msg " already running"
        log_end_msg 0
        ;;
  stop)
        log_daemon_msg "Stopping $DESC: $NAME"
        start-stop-daemon --oknodo --stop --quiet --pidfile $PIDFILE \
                --exec $DAEMON
        log_end_msg 0
        ;;

Best Answer

With --signal you can use numerical values, or the name of the signal without the SIG part, e.g. --signal TERM.

Don't use --exec $DAEMON when stopping, as this doesn't work if you're trying to stop the daemon after having replaced the executable as part of an upgrade: the executable doesn't match the running process, so it doesn't get stopped. Alternatively if you insist on using --exec make sure to stop the daemon before replacing the executable.

If the pidfile created doesn't match the process of the daemon, then probably the daemon itself also forks into the background. If that's the case, then see if the daemon has an option to prevent this backgrounding, as start-stop-daemon already takes care of this. If this is impossible, don't use the pidfile option and rely on --exec (but see the previous paragraph about upgrading).