Skip to content

Instantly share code, notes, and snippets.

@Neil-Smithline
Created May 29, 2012 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Neil-Smithline/2821946 to your computer and use it in GitHub Desktop.
Save Neil-Smithline/2821946 to your computer and use it in GitHub Desktop.
Quickie /etc/init.d script to get a daemon running.
#! /bin/sh
### BEGIN INIT INFO
# Provides: bitlbee
# Required-Start:
# Required-Stop:
# Should-Start:
# Default-Start: S
# Default-Stop:
# Short-Description: Run Bitlbee IRC server/IM gateway.
### END INIT INFO
EXE=/usr/local/sbin/bitlbee
NAME=bitlbee
PIDFILE=/var/run/$NAME.pid
PIDARG="-P " # Must end with a space if it is required
ARGS=""
PATH=/sbin:/bin
. /lib/init/vars.sh
. /lib/lsb/init-functions
do_start () {
$EXE $PIDARG$PIDFILE $ARGS
echo "$NAME running (pid=$(cat $PIDFILE))"
}
do_stop () {
echo "Trying to kill $NAME (pid=$(cat $PIDFILE))"
kill $(cat $PIDFILE)
}
case "$1" in
start)
do_start
;;
stop)
do_stop
;;
restart)
do_stop
sleep 5
do_start
;;
reload|force-reload)
echo "Error: argument '$1' not supported" >&2
exit 3
;;
*)
echo "Usage: $NAME [start|stop|restart]" >&2
exit 3
;;
esac
@Neil-Smithline
Copy link
Author

At least on Ubuntu, daemons are moving towards initctl(8) for daemon process management. But sometimes it is just easy to whip up a quick script to start your daemon. This is the script I use. It is configured for Bitlbee but the script has been designed to be easy to modify. You should only need to edit data in the first 16 lines (if you don't know what it does, don't touch it).

The only tricky part is that the script does not add a space between the PIDFILE and the PIDARG command line switch to pass the PIDFILE in. This is so the script can handle new-style arguments such as --pidfile= as well as old-style arguments such as -P. If you need to use an old-style argument, be sure that PIDFILE ends with a space.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment