I am writing an /etc/init.d/mydaemon:
# ...
source functions # LSB compliant
EXEC=/usr/local/bin/mydaemon
PROG=mydaemon
function start() {
daemon --pidfile=/var/run/mydeamon.pid ${EXEC}
}
function stop() {
killproc ${PROG}
}
# ...
my /usr/local/bin/mydaemon:
#!/bin/bash
trap "trap TERM ; kill 0" TERM
binary with some args
AFAIK, this should work because:
daemonrecords themydaemon's PID in/var/run/mydaemon.pidkillprocread that PID and sendSIGTERMto that PID.mydaemontrap this signal, disable the trap and sendSIGTERMto the entirePGRP, including the process ofbinary with some args.
However this doesn't work. After stopping the service, mydaemon terminates, but binary is still running.
What am I missing, and what is the best practice for stopping the daemon and all its' children?
BTW:
When my /usr/local/bin/mydaemon is:
#!/bin/bash
binary with some args &
echo $! $$ > /var/run/mydaemon.pid
wait
It works properly, but this seems less robust to me, and there are times where this is not appropriate (when the binary invocation is less straight forward, or it has it's own children, etc).
If you give the parent process' id to pkill, it'll kill all the children: