#!/bin/sh
#
# Mosquitto daemon control script.
# Written for Slackware Linux by Jose Bovet Derpich <jose.bovet@gmail.com>.

BIN=/usr/sbin/mosquitto
CONF=/etc/mosquitto/mosquitto.conf
# enable pid_file in /etc/mosquitto/mosquitto.conf
PID=/var/run/mosquitto.pid

mosquitto_start() {
  if [ ! -r $CONF ]; then # no config file, exit:
    echo "$CONF does not appear to exist. Abort."
    exit 1
  fi

  if [ -s $PID ]; then
    echo "Mosquitto appears to already be running?"
    exit 1
  fi

  echo "Starting Mosquitto server daemon..."
  if [ -x $BIN ]; then
    $BIN -c $CONF -d
  fi
}

mosquitto_stop() {
  echo "Shutdown Mosquitto..."
  if [ -r $PID ]; then
    kill -TERM $(cat $PID)
    rm $PID
  fi
}

mosquitto_restart() {
  mosquitto_stop
  sleep 3
  mosquitto_start
}

case "$1" in
  start)
    mosquitto_start
    ;;
  stop)
    mosquitto_stop
    ;;
  restart)
    mosquitto_restart
    ;;
  *)
  echo "usage: `basename $0` {start|stop|restart}"
esac

Gist Link


Jose Bovet Derpich

Software Engineer