1

I am using a start-stop-daemon to make a INIT script for my script. I am using --make-pidfile cause my script doesn't make its own pid. I can start my script using start and pid file generates with appropriate PID. But the stop function doesn't work. I am getting return code 0 with --oknodo and 1 without it. If I do

ps -ef | grep perl

and

cat /home/me/mydaemon/run

I always see the same PID. I can terminate the script using

kill -15 PID. 

But not with the stop function of my init script.

What is the proper way to stop my process?

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 didn't find find any documentation for --signal itself. Like how to specify --signal if I want to send a SIGTERM.

#!/bin/sh
### BEGIN INIT INFO
# Provides:          myd
# Required-Start:    $local_fs $network $named $time $syslog
# Required-Stop:     $local_fs $network $named $time $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Description:       Diitalk daemon for sending push notifications
### END INIT INFO

. /lib/lsb/init-functions

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON="/home/me/mydaemon/myd"
NAME="myd"
DESC="My Daemon"
HOMEDIR=/home/me/mydaemon/run
PIDFILE="$HOMEDIR/$NAME.pid"
USER=me
GROUP=me
SHM_MEMORY=64
PKG_MEMORY=8
DUMP_CORE=no

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
        echo $?
        log_end_msg 0
        ;;
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Kamrul Khan
  • 3,260
  • 4
  • 32
  • 59
  • The argument to `--signal` is either going to be the signal name (`TERM`) or the signal number (`15`) I assume but the man page seems to indicate that `TERM` is the default. What about your stop command there doesn't work? What's the return code of that call? – Etan Reisner Jul 15 '15 at 18:43
  • I am getting 0 with --oknodo – Kamrul Khan Jul 15 '15 at 19:09
  • And without `--oknodo`? 1? Is your process still running? Is the pid in the pidfile correct? – Etan Reisner Jul 15 '15 at 19:11
  • yes with and without --oknodo my process still runs. I checked my pid file and it always contains the right pid (the pid of my script). I can terminate my script using kill -15 though – Kamrul Khan Jul 15 '15 at 19:19
  • Does your daemon run with the name in `$DAEMON` or does it exec/fork/etc. and change names? If you run with `−−test` what does it say it is going to do? Does adding `--verbose` to the call say anything useful? – Etan Reisner Jul 15 '15 at 19:24
  • it runs with something like /usr/bin/perl $DAEMON. Didnt get anything adding --test and verbose. I updated my post.. please check – Kamrul Khan Jul 15 '15 at 19:45
  • The only thing I can think to suggest at this point is to run `start-stop-daemon` under `strace` (or similar) and see what it is actually doing (and not doing). `strace -o /tmp/daemon.strace -f start-stop-daemon ....` – Etan Reisner Jul 15 '15 at 20:01
  • got the issue. Thanks a lot for your help mate :) – Kamrul Khan Jul 15 '15 at 21:38

1 Answers1

1

The issue was with the --exec that I used for matching the process name. As per the start-stop-daemon documentation :

   -x, --exec executable
          Check  for  processes  that  are  instances  of  this executable
          (according to /proc/pid/exe).

In my case as my script is Perl script, /proc/pid/exe is symlinked to /usr/bin/perl; therefore the exec couldnt match the process name. I removed the exec so that it matches only the PID. Now I can properly stop my process.

Kamrul Khan
  • 3,260
  • 4
  • 32
  • 59
  • Going purely by pid is, theoretically, unsafe. You should probably use `-x /usr/bin/perl` or similar to at least attempt to be sure it is the right thing you are killing. – Etan Reisner Jul 15 '15 at 21:48