Skip to content

Instantly share code, notes, and snippets.

@karlp
Created August 25, 2011 14:02
Show Gist options
  • Save karlp/1170734 to your computer and use it in GitHub Desktop.
Save karlp/1170734 to your computer and use it in GitHub Desktop.
using daemon to make a respawning mosquitto server
#! /bin/bash
# Based on /etc/init.d/sshd as it came on CentOS 5.6:
#
# Copyright (c) 1995-2000 SuSE GmbH Nuernberg, Germany.
#
# Author: Jiri Smid <[email protected]>
# /etc/init.d/mosquitto
#
### BEGIN INIT INFO
# Provides: mosquitto
# Required-Start: $network $remote_fs
# Required-Stop: $network $remote_fs
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: Mosquitto MQTT broker
# Description: Mosquitto MQTT broker
### END INIT INFO
MOSQUITTO_BIN=/usr/sbin/mosquitto
test -x $MOSQUITTO_BIN || exit 5
prog=mosquitto
LOGFILE=/var/log/$prog/$prog.log
PID_FILE=/var/run/$prog/$prog.pid
RUNAS=$prog
. /etc/rc.d/init.d/functions
start()
{
echo -n "Starting Mosquitto MQTT broker"
## Start daemon with startproc(8). If this fails
## the echo return value is set appropriate.
/usr/local/bin/daemon -n $prog --verbose -r -o $LOGFILE -u $RUNAS -F $PID_FILE -- $MOSQUITTO_BIN -c /etc/mosquitto/mosquitto.conf && success || failure
RETVAL=$?
echo
}
stop()
{
echo -n "Shutting down Mosquitto MQTT broker"
## Stop daemon with killproc(8) and if this fails
## set echo the echo return value.
/usr/local/bin/daemon -F $PID_FILE -n $prog --verbose --stop && success || failure
RETVAL=$?
echo
}
reload()
{
echo -n $"Reloading $prog: "
# FIXME!!!
echo "Actually, this won't work, it just kills the service"
killproc -p $PID_FILE -HUP
RETVAL=$?
echo
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
## Stop the service and regardless of whether it was
## running or not, start it again.
$0 stop
$0 start
;;
force-reload|reload)
reload
;;
status)
/usr/local/bin/daemon -F $PID_FILE -n $prog --verbose --running
RETVAL=$?
;;
*)
echo "Usage: $0 {start|stop|status|restart|force-reload|reload}"
exit 1
;;
esac
exit $RETVAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment