The answer is to write a startup script for your daemon program.
For Redhat distribution, refer to the following as template:
#!/bin/sh
# Service descriptions and general comments
#
# chkconfig: <runlevel list> <start priority> <end priority>
# description: <multi-line description of service>
#
# processname: <shown in ps>
# pidfile: /var/run/$NAME.pid
#
# Source function library: daemon(), killproc() and status()
. /etc/rc.d/init.d/functions
NAME=$(basename "$0")
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
SERVICE="My Service"
DAEMON="/path/to/my/daemon -s arg0 -f arg1"
RETVAL=0
test -x "$DAEMON" || exit 0
case "$1" in
start)
echo -n "Starting $SERVICE ..."
daemon $DAEMON
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$NAME
;;
stop)
echo -n "Stopping $SERVICE daemon: "
killproc $NAME
RETVAL=$?
rm -f /var/lock/subsys/$NAME && rm -f /var/lock/$NAME
echo
;;
status)
status $NAME
RETVAL=$?
;;
restart|force-reload)
$0 stop
$0 start
RETVAL=$?
;;
*)
printf "Usage: %q {start|stop|status|restart|force-reload}\n" "$0" >&2
exit 1
;;
esac
exit $RETVAL
Then type the following:#Copy your startup script as /etc/init.d/daemon:
cp daemon /etc/init.d/daemon
#Let it executable:
chmod +x /etc/init.d/daemon
#Register the service:
chkconfig --add daemon
For Debian variants, refer to the following as template:
#!/bin/sh
NAME=$(basename "$0")
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
SERVICE="My Service"
DAEMON="/path/to/my/daemon -s arg0 -f arg1"
USER=user
RETVAL=0
test -x "$DAEMON" || exit 0
case "$1" in
start)
echo -n "Starting $SERVICE ... "
su "$USER" -c "$(printf "%q -f" "$DAEMON" )"
echo "$NAME."
;;
stop)
echo -n "Stopping $SERVICE ... "
killall --quiet "$DAEMON"
echo "$NAME."
;;
restart|force-reload)
echo -n "Restarting $SERVICE ... "
killall --quiet "$DAEMON"
sleep 1
su "$USER" -c "$(printf "%q -f" "$DAEMON" )"
;;
*)
printf "Usage: %q {start|stop|restart|force-reload}\n" "$0" >&2
exit 1
;;
esac
exit 0
Then type the following:#Copy your startup script as /etc/init.d/daemon:
cp daemon /etc/init.d/daemon
#Let it executable:
chmod +x /etc/init.d/daemon
#Register it to run at startup:
# on debian distros:
update-rc.d daemon defaults
# on a generic distro
ln -s /etc/init.d/daemon /etc/rc0.d/K20daemon
ln -s /etc/init.d/daemon /etc/rc1.d/K20daemon
ln -s /etc/init.d/daemon /etc/rc6.d/K20daemon
ln -s /etc/init.d/daemon /etc/rc4.d/S20daemon
ln -s /etc/init.d/daemon /etc/rc5.d/S20daemon
References
FAQ amuled
Writing System V init scripts for Red Hat Linux
Linux: How to write a System V init script
No comments:
Post a Comment