I\'d like a daemonizer that can turn an arbitrary, generic script or command into a daemon.
There are two common cases I\'d like to deal with:
I hav
You should have a look at daemonize. It allows to detect second copy (but it uses file locking mechanism). Also it works on different UNIX and Linux distributions.
If you need to automatically start your application as daemon, then you need to create appropriate init-script.
You can use the following template:
#!/bin/sh
#
# mydaemon This shell script takes care of starting and stopping
# the
#
# Source function library
. /etc/rc.d/init.d/functions
# Do preliminary checks here, if any
#### START of preliminary checks #########
##### END of preliminary checks #######
# Handle manual control parameters like start, stop, status, restart, etc.
case "$1" in
start)
# Start daemons.
echo -n $"Starting daemon: "
echo
daemon
echo
;;
stop)
# Stop daemons.
echo -n $"Shutting down : "
killproc
echo
# Do clean-up works here like removing pid files from /var/run, etc.
;;
status)
status
;;
restart)
$0 stop
$0 start
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
exit 1
esac
exit 0