title: Letting other users stop/restart simple bash daemons – use signals or what?
body: <p>I have a web server where I run some slow-starting programs as daemons. These sometimes need quick restarting (or stopping) when I recompile them or switch to another installation of them.</p>
  
  <p>Inspired by <a href="http://mywiki.wooledge.org/ProcessManagement" rel="noreferrer">http://mywiki.wooledge.org/ProcessManagement</a>, I'm writing a script
  called <code>daemonise.sh</code> that looks like</p>
  
  <pre><code>#!/bin/sh
  while :; do
      ./myprogram lotsadata.xml
      echo "Restarting server..." 1&gt;&amp;2
  done
  </code></pre>
  
  <p>to keep a "daemon" running. Since I sometimes need to stop it, or just
  restart it, I run that script in a screen session, like:</p>
  
  <pre><code>$ ./daemonise.sh &amp; DPID=$!
  $ screen -d
  </code></pre>
  
  <p>Then perhaps I recompile myprogram, install it to a new path, start
  the new one up and want to kill the old one:</p>
  
  <pre><code>$ screen -r
  $ kill $DPID
  $ screen -d
  </code></pre>
  
  <p>This works fine when I'm the only maintainer, but now I want to let
  someone else stop/restart the program, no matter who started it. And
  to make things more complicated, the <code>daemonise.sh</code> script in fact
  starts about 16 programs, making it a hassle to kill every single one
  if you don't know their PIDs.</p>
  
  <p>What would be the "best practices" way of letting another user
  stop/restart the daemons?</p>
  
  <p>I thought about shared screen sessions, but that just sounds hacky and
  insecure. The best solution I've come up with for now is to wrap
  starting and killing in a script that catches certain signals:</p>
  
  <pre><code>#!/bin/bash
  DPID=
  trap './daemonise.sh &amp; DPID=$!' USR1
  trap 'kill $DPID' USR2 EXIT
  
  # Ensure trapper wrapper doesn't exit:
  while :; do
      sleep 10000 &amp; wait $!
  done
  </code></pre>
  
  <p>Now, should another user need to stop the daemons and I can't do it,
  she just has to know the pid of the wrapper, and e.g. <code>sudo kill -s
  USR2 $wrapperpid</code>. (Also, this makes it possible to run the daemons 
  on reboots, and still kill them cleanly.)</p>
  
  <p>Is there a better solution? Are there obvious problems with this
  solution that I'm not seeing? </p>
  
  <p>(After reading Greg's Bash Wiki, I'd like to avoid any solution involving pgrep or PID-files …)</p>