Skip to content

Instantly share code, notes, and snippets.

@clone1018
Created April 26, 2011 23:08
Show Gist options
  • Save clone1018/943388 to your computer and use it in GitHub Desktop.
Save clone1018/943388 to your computer and use it in GitHub Desktop.
### BEGIN INIT INFO
# Provides: quasselcore
# Required-Start: $network $local_fs
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: distributed IRC client using a central core component
# Description: This is the core component of Quassel. A modern,
# cross-platform, distributed IRC client, meaning that one
# (or multiple) client(s) can attach to and detach from this
# central core. It's much like the popular combination
# of screen and a text-based IRC client such as WeeChat.
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/bin/quasselcore
NAME=quasselcore
DESC="distributed IRC core"
LOGDIR=/var/log/quassel
PORT=4242
PIDFILE=/var/run/quasselcore.pid
test -x $DAEMON || exit 0
. /lib/lsb/init-functions
DAEMON_OPTS=""
DIETIME=10
STARTIME=3
LOGFILE=$LOGDIR/core.log
DATADIR=/var/cache/quassel
DAEMONUSER=quasselcore
if [ -f /etc/default/$NAME ] ; then
. /etc/default/$NAME
fi
if [ -n "$DAEMONUSER" ] ; then
if getent passwd | grep -q "^$DAEMONUSER:"; then
# Obtain the uid and gid
DAEMONUID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $3}'`
DAEMONGID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $4}'`
else
log_failure_msg "The user $DAEMONUSER, required to run $NAME does not exist."
exit 1
fi
fi
set -e
running_pid() {
pid=$1
name=$2
[ -z "$pid" ] && return 1
[ ! -d /proc/$pid ] && return 1
cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
[ "$cmd" != "$name" ] && return 1
return 0
}
running() {
[ ! -f "$PIDFILE" ] && return 1
pid=`cat $PIDFILE`
running_pid $pid $DAEMON || return 1
return 0
}
start_server() {
start-stop-daemon --start --quiet --pidfile $PIDFILE --make-pidfile \
--background --chuid $DAEMONUSER --exec $DAEMON \
-- --logfile=$LOGFILE --loglevel=$LOGLEVEL --configdir=$DATADIR \
--port=$PORT \
$DAEMON_OPTS
errcode=$?
return $errcode
}
stop_server() {
start-stop-daemon --stop --quiet --pidfile $PIDFILE --user $DAEMONUSER \
--exec $DAEMON
errcode=$?
return $errcode
}
force_stop() {
[ ! -e "$PIDFILE" ] && return
if running ; then
kill -15 $pid
# Is it really dead?
sleep "$DIETIME"s
if running ; then
kill -9 $pid
sleep "$DIETIME"s
if running ; then
echo "Cannot kill $NAME (pid=$pid)!"
exit 1
fi
fi
fi
rm -f $PIDFILE
}
case "$1" in
start)
log_daemon_msg "Starting $DESC" "$NAME"
# Check if it's running first
if running ; then
log_progress_msg "apparently already running"
log_end_msg 0
exit 0
fi
if start_server ; then
[ -n "$STARTTIME" ] && sleep $STARTTIME # Wait some time
if running ; then
log_end_msg 0
else
log_end_msg 1
fi
else
log_end_msg 1
fi
;;
stop)
log_daemon_msg "Stopping $DESC" "$NAME"
if running ; then
stop_server
log_end_msg $?
else
log_progress_msg "apparently not running"
log_end_msg 0
exit 0
fi
;;
force-stop)
$0 stop
if running; then
# If it's still running try to kill it more forcefully
log_daemon_msg "Stopping (force) $DESC" "$NAME"
force_stop
log_end_msg $?
fi
;;
restart|force-reload)
log_daemon_msg "Restarting $DESC" "$NAME"
stop_server
# Wait some sensible amount, some server need this
[ -n "$DIETIME" ] && sleep $DIETIME
start_server
[ -n "$STARTTIME" ] && sleep $STARTTIME
running
log_end_msg $?
;;
status)
log_daemon_msg "Checking status of $DESC" "$NAME"
if running ; then
log_success_msg "running"
log_end_msg 0
else
log_success_msg "apparently not running"
log_end_msg 1
exit 1
fi
;;
reload)
log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon"
log_warning_msg "cannot re-read the config file (use restart)."
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2
exit 1
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment