Skip to content

Instantly share code, notes, and snippets.

@a740122
Forked from DaveThw/mosquitto
Created March 25, 2017 13:05
Show Gist options
  • Select an option

  • Save a740122/9c4d0f9ba0b87674fe1d3ef01ea1cc88 to your computer and use it in GitHub Desktop.

Select an option

Save a740122/9c4d0f9ba0b87674fe1d3ef01ea1cc88 to your computer and use it in GitHub Desktop.

Revisions

  1. @DaveThw DaveThw revised this gist Jan 31, 2016. No changes.
  2. @DaveThw DaveThw revised this gist Jan 31, 2016. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions mosquitto
    Original file line number Diff line number Diff line change
    @@ -9,10 +9,10 @@
    ### END INIT INFO

    # Can be downloaded and installed in one go by using this command
    # sudo wget -O /tmp/download https://gist.github.com/DaveThw/????/download && sudo tar -zxf /tmp/download --strip-components 1 -C /etc/init.d && sudo chmod +x /etc/init.d/mosquitto && sudo update-rc.d mosquitto defaults
    # sudo wget -O /tmp/download https://gist.github.com/DaveThw/4396124291bb4f92b427/download && sudo tar -zxf /tmp/download --strip-components 1 -C /etc/init.d && sudo chmod +x /etc/init.d/mosquitto && sudo update-rc.d mosquitto defaults

    # Or download and just move to /etc/init.d using this command
    # sudo wget -O /tmp/download https://gist.github.com/DaveThw/????/download && sudo tar -zxf /tmp/download --strip-components 1 -C /etc/init.d && sudo chmod +x /etc/init.d/mosquitto
    # sudo wget -O /tmp/download https://gist.github.com/DaveThw/4396124291bb4f92b427/download && sudo tar -zxf /tmp/download --strip-components 1 -C /etc/init.d && sudo chmod +x /etc/init.d/mosquitto
    # and then start/stop/restart the service with these commands
    # sudo service mosquitto stop
    # sudo service mosquitto start
    @@ -107,4 +107,4 @@ case "$1" in
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
    esac
    exit 0
    exit 0
  3. @DaveThw DaveThw created this gist Jan 31, 2016.
    110 changes: 110 additions & 0 deletions mosquitto
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,110 @@
    #!/bin/bash
    ### BEGIN INIT INFO
    # Provides: mosquitto
    # Required-Start: $local_fs $remote_fs $network
    # Required-Stop: $local_fs $remote_fs $network
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: Start or stop the mosquitto service
    ### END INIT INFO

    # Can be downloaded and installed in one go by using this command
    # sudo wget -O /tmp/download https://gist.github.com/DaveThw/????/download && sudo tar -zxf /tmp/download --strip-components 1 -C /etc/init.d && sudo chmod +x /etc/init.d/mosquitto && sudo update-rc.d mosquitto defaults

    # Or download and just move to /etc/init.d using this command
    # sudo wget -O /tmp/download https://gist.github.com/DaveThw/????/download && sudo tar -zxf /tmp/download --strip-components 1 -C /etc/init.d && sudo chmod +x /etc/init.d/mosquitto
    # and then start/stop/restart the service with these commands
    # sudo service mosquitto stop
    # sudo service mosquitto start
    # sudo service mosquitto restart
    # And then, if/when you want the service to autostart on bootup, use this
    # sudo update-rc.d mosquitto defaults
    # Conversely, if you want to stop the service autostarting on bootup, use this
    # sudo update-rc.d -f mosquitto remove

    # This script is based on my version of the Node-RED init.d script, here: https://gist.github.com/DaveThw/da394c8d04fa2bba55ac


    # User that launches mosquitto (it's advised to create a new user)
    # You can do:
    # sudo useradd mosquitto
    # then change line below from USER=pi to USER=mosquitto
    USER=pi

    # Location of the mosquitto configuration file
    CONFIG="/etc/mosquitto/mosquitto.conf"



    # DON'T CHANGE anything below this line unless you know what you're doing!


    NAME=mosquitto
    DAEMON="/usr/local/sbin/mosquitto"
    OPTIONS="-c ${CONFIG}"
    LOG="/var/log/${NAME}.log"
    PIDFILE="/var/run/${NAME}.pid"

    . /lib/lsb/init-functions

    start_daemon () {
    # If the log file doesn't yet exist, attempt to create it
    ! [ -e "$LOG" ] && sudo touch "$LOG"
    # Check if USER is the owner of the log file, and chown if not
    [ -e "$LOG" ] && [ -z "$(find "$LOG" -user $USER)" ] && sudo chown $USER "$LOG"

    echo >> $LOG
    date >> $LOG
    echo "Starting daemon $NAME with command '$DAEMON' and options '$OPTIONS'..." >> $LOG
    echo >> $LOG

    start-stop-daemon --start --background \
    --chuid $USER \
    --name $NAME \
    --make-pidfile --pidfile $PIDFILE \
    --startas /bin/bash -- -c "exec $DAEMON $OPTIONS >> $LOG 2>&1"
    # check if the daemon actually started, and return an appropriate result with log_end_msg
    status="0"
    pidofproc $DAEMON >/dev/null || status="$?"
    log_end_msg $status
    }

    stop_daemon () {
    echo >> $LOG
    date >> $LOG
    echo "Stopping daemon $NAME..." >> $LOG
    echo >> $LOG

    status="0"
    start-stop-daemon --stop --signal INT --quiet \
    --chuid $USER \
    --exec $DAEMON --pidfile $PIDFILE --retry 30 \
    --oknodo || status="$?"
    log_end_msg $status
    }

    case "$1" in
    start)
    log_daemon_msg "Starting daemon" "$NAME"
    start_daemon
    ;;
    stop)
    log_daemon_msg "Stopping daemon" "$NAME"
    stop_daemon
    ;;
    restart)
    log_daemon_msg "Stopping daemon" "$NAME"
    stop_daemon
    sleep 5
    log_daemon_msg "Re-Starting daemon" "$NAME"
    start_daemon
    ;;
    status)
    status_of_proc "$DAEMON" "$NAME"
    exit $?
    ;;
    *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
    esac
    exit 0