Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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:

kill -TERM -$(cat /var/run/my_shell_daemon.pid)
share|improve this answer

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.

share|improve this answer
    
Does the kill on a PID shut down all other commands? For instance, a 'cat /tmp/foo' command might still continue, called from within /usr/sbin/foo, right? –  ServerChecker Jun 7 '11 at 7:09
    
For more information on that, please read up on linux signal handling. In general, yes, sending a KILL signal to the parent will work. –  dmourati Jun 7 '11 at 15:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.