I have a web server where I run programs with slow startup as daemons. Sometimes they need a quick restart (or stop) when recompiling or switching to another installation.
Inspired by http://mywiki.wooledge.org/ProcessManagement , I am writing a script called daemonise.sh
that looks like
#!/bin/sh while :; do ./myprogram lotsadata.xml echo "Restarting server..." 1>&2 done
to run the "daemon". Since I sometimes need to stop this or just restart it, I ran this script in a screen session, for example:
$ ./daemonise.sh & DPID=$! $ screen -d
Then, perhaps, I will recompile myprogram, install it on a new path, start a new one and want to kill the old one:
$ screen -r $ kill $DPID $ screen -d
This works great when I'm the only maintainer, but now I want to let someone stop / restart the program, regardless of who started it. And also to make things more complex, the daemonise.sh
script actually runs about 16 programs, which makes them troublesome to kill everyone if you don't know their PID.
What would be the βbest wayβ to allow another user to stop / restart daemons?
I thought about general screen sessions, but it sounds just hacked and insecure. The best solution I came up with is to wrap the start and kill in a script that captures certain signals:
#!/bin/bash DPID= trap './daemonise.sh & DPID=$!' USR1 trap 'kill $DPID' USR2 EXIT
Now, if another user needs to stop the daemons, and I cannot do this, she just needs to know the pid of the wrapper and, for example, sudo kill -s USR2 $wrapperpid
. (In addition, it allows you to run daemons on reboots and still kill them cleanly.)
Is there a better solution? Are there obvious problems with this solution that I don't see?
(After reading the Greg Bash Wiki, I would like to avoid any solution containing pgrep or PID files ...)