title: kill all subprocesses of a daemon
body: <p>I am writing an <code>/etc/init.d/mydaemon</code>:</p>
  
  <pre><code># ...
  source functions # LSB compliant 
  
  EXEC=/usr/local/bin/mydaemon
  PROG=mydaemon
  
  function start() {
    daemon --pidfile=/var/run/mydeamon.pid ${EXEC}
  }
  
  function stop() {
    killproc ${PROG}
  }
  
  # ...
  </code></pre>
  
  <p>my <code>/usr/local/bin/mydaemon</code>:</p>
  
  <pre><code>#!/bin/bash
  trap "trap TERM ; kill 0" TERM
  binary with some args
  </code></pre>
  
  <p>AFAIK, this should work because:</p>
  
  <ul>
  <li><code>daemon</code> records the <code>mydaemon</code>'s PID in <code>/var/run/mydaemon.pid</code></li>
  <li><code>killproc</code> read that PID and send <code>SIGTERM</code> to that PID.</li>
  <li><code>mydaemon</code> trap this signal, disable the trap and send <code>SIGTERM</code> to the entire <code>PGRP</code>, including the process of <code>binary with some args</code>.</li>
  </ul>
  
  <p>However this doesn't work. After stopping the service, <code>mydaemon</code> terminates, but <code>binary</code> is still running.</p>
  
  <p>What am I missing, and <strong>what is the best practice for stopping the daemon and all its' children?</strong></p>
  
  <hr>
  
  <p>BTW:</p>
  
  <p>When my /usr/local/bin/mydaemon is:</p>
  
  <pre><code>#!/bin/bash
  binary with some args &amp;
  echo $! $$ &gt; /var/run/mydaemon.pid
  wait
  </code></pre>
  
  <p>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).</p>