What is the proper way to terminate a script using

2019-09-04 04:35发布

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
        ;;

1条回答
一夜七次
2楼-- · 2019-09-04 04:58

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.

查看更多
登录 后发表回答