In debian/Ubuntu, install "runit".
then in /etc/service directory, create your user service directory. In this example, we use "nc" (netcat listen)
cd /etc/service; mkdir nc
now create a file called "run", with the following content:
#!/bin/sh
d=`date`;
DIR=$(dirname $(readlink -f "$0"))
name=$(basename "$DIR")
echo "$d service $name started" >> /var/log/runit.log
exec nc -l -p 8889
Only the first line and last line are must have. The mittle 4 lines are for logging purpose.
Now "chmod +x run"
Now nc should be running (automatically picked up by runsvdir which scans /etc/service directory for changes). list of commands:
sv status nc
sv stop nc (or sv down nc)
sv start nc (or sv up nc)
sv restart nc
sv reload nc (send HUP signal)
sv status /etc/service/* (check all service status)
touch a file "nc/down" to stop the auto-restart
create a file "nc/finish" with the following content:
#!/bin/sh
d=`date`;
DIR=$(dirname $(readlink -f "$0"))
name=$(basename "$DIR")
echo "$d service $name stopped" >> /var/run/runit.log
This script is run every time nc exits.
Internally, 3 core executables: sv, runsv (the actual daemon monitor), and runsvdir (monitors the entire /etc/service directory)