On Ubuntu, I copied /etc/init.d/
skeleton as a new file and made it "all" executable (just like the others in that directory). In it, I made a "foo" that was just another Bash script in /usr/sbin
, but it was special in that it sits in a loop waiting on a named pipe connection from a PHP web page. (More details on that are here, although that's a side topic.)
Because it has to wait in a loop on a named pipe connection, this hung the command up at command line obviously. Not a big deal, because I actually want it to hang and wait on a new connection. But I don't want it happening in the foreground. So, I made start-stop-daemon utilize the --background option.
Okay, it works great for starting and status, but stopping doesn't work -- it leaves the command in memory, as well as the cat /tmp/pipe
command I have in my /usr/sbin/foo
Bash script. They warn that this could happen when using the --background option on the start-stop-daemon command.
THE QUESTION
So, what's the right way to properly stop /usr/sbin/foo when it's run with start-stop-daemon --background
, along with stop any child processes (the cat /tmp/pipe command, for instance) that might be called by /usr/sbin/foo? I mean, I guess I could use killall, but that's kind of deadly. Perhaps you know of a more efficient way?
I'd recommend consulting the LSB:
http://refspecs.freestandards.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/tocsysinit.html
Here's a Debian-centric wiki on the same that may also be useful:
http://wiki.debian.org/LSBInitScripts
You typically stash a pid file somewhere under /var/run and then explicitly kill that command on stop.
I think in that situation the shell will be the leader of the process group containing the various commands run by the script. You can kill a process group by sending a signal to the opposite of the PID of the process group: