Mosquitto Installation

I recently setup MQTT on my OpenHab system and everything is working fine. My only problem is that I am unable to get the mosquitto program to start at boot. I run OpenHab and the Mosquitto on my computer running Ubuntu 16.04. I installed the Mosquitto program using openhabian-config. I tried to do the “sudo systemctl enable mosquitto.service”, but it didn’t work. Here is the message I got:

Synchronizing state of mosquitto.service with SysV init with /lib/systemd/systemd-sysv-install…
Executing /lib/systemd/systemd-sysv-install enable mosquitto
Failed to execute operation: Bad message

The only way I can get it to start is to open a terminal screen when my computer starts up and type “mosquitto” to get it to run.
Any help to get mosquitto to start automatically when the computer is started would be greatly appreciated.
Thanks!

As mosquitto comes with a sysv init script, systemd has to build an automatic service script.
This is the script in my debian stretch system:

cat /run/systemd/generator.late/mosquitto.service
# Automatically generated by systemd-sysv-generator

[Unit]
Documentation=man:systemd-sysv-generator(8)
SourcePath=/etc/init.d/mosquitto
Description=LSB: mosquitto MQTT v3.1 message broker
Before=multi-user.target
Before=multi-user.target
Before=multi-user.target
Before=graphical.target
After=remote-fs.target

[Service]
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
SuccessExitStatus=5 6
ExecStart=/etc/init.d/mosquitto start
ExecStop=/etc/init.d/mosquitto stop
ExecReload=/etc/init.d/mosquitto reload

Maybe it will suffice to paste this file into /etc/systemd/system/mosquitto.service and afterwards do a

sudo systemctl daemon-reload

But be aware that a script mosquitto.service is already at your system, so you will have to remove the autogenerated file first. Or maybe as a first step, take a look at /etc/init.d/mosquitto :slight_smile: This script should start and stop mosquitto without problems:

sudo /etc/init.d/mosquitto start

Hi Udo_Hartmann
Thanks for helping me trying to fix this problem. I copied your script to my system and issued the command “sudo systemctl daemon-reload”. I rebooted but still mosquitto is not starting automatically at boot up.
I then took a look at the script in “/etc/init.d/mosquitto” this is what it contains:

  GNU nano 2.5.3                                                                  File: mosquitto

#! /bin/sh

### BEGIN INIT INFO
# Provides:             mosquitto
# Required-Start:       $remote_fs $syslog
# Required-Stop:        $remote_fs $syslog
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    mosquitto MQTT v3.1 message broker
# Description:
#  This is a message broker that supports version 3.1 of the MQ Telemetry
#  Transport (MQTT) protocol.
#
#  MQTT provides a method of carrying out messaging using a publish/subscribe
#  model. It is lightweight, both in terms of bandwidth usage and ease of
#  implementation. This makes it particularly useful at the edge of the network
#  where a sensor or other simple device may be implemented using an arduino for
#  example.
### END INIT INFO

set -e

 PIDFILE=/var/run/mosquitto.pid
DAEMON=/usr/sbin/mosquitto

# /etc/init.d/mosquitto: start and stop the mosquitto MQTT message broker

test -x ${DAEMON} || exit 0

umask 022

. /lib/lsb/init-functions

 # Are we running from init?
run_by_init() {
    ([ "$previous" ] && [ "$runlevel" ]) || [ "$runlevel" = S ]
}

export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"

case "$1" in
  start)
        if init_is_upstart; then
            exit 1
        fi
        log_daemon_msg "Starting network daemon:" "mosquitto"
        if start-stop-daemon --start --quiet --oknodo --background  --make-pidfile --pidfile ${PIDFILE} --exec      ${DAEMON} -- -c /etc/mosquitto/mosquitto.conf ; then
            log_end_msg 0
        else
            log_end_msg 1
        fi
        ;;
  stop)
        if init_is_upstart; then
            exit 0
        fi
        log_daemon_msg "Stopping network daemon:" "mosquitto"
        if start-stop-daemon --stop --quiet --oknodo --pidfile ${PIDFILE}; then
            log_end_msg 0
            rm -f ${PIDFILE}

I issued the command “sudo /etc/init.d/mosquitto start” . It didn’t seem to work. My raspberry pi logs are saying: “Starting MQTT broker connection 'broker” repeatedly.
Any other ideas?

Well, this is a bit different to the debian script:

udo1toni@openhab2:~$ cat /etc/init.d/mosquitto
#! /bin/sh

### BEGIN INIT INFO
# Provides:             mosquitto
# Required-Start:       $remote_fs $syslog
# Required-Stop:        $remote_fs $syslog
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    mosquitto MQTT v3.1 message broker
# Description:
#  This is a message broker that supports version 3.1 of the MQ Telemetry
#  Transport (MQTT) protocol.
#
#  MQTT provides a method of carrying out messaging using a publish/subscribe
#  model. It is lightweight, both in terms of bandwidth usage and ease of
#  implementation. This makes it particularly useful at the edge of the network
#  where a sensor or other simple device may be implemented using an arduino for
#  example.
### END INIT INFO

set -e

PIDFILE=/var/run/mosquitto.pid
DAEMON=/usr/sbin/mosquitto

# /etc/init.d/mosquitto: start and stop the mosquitto MQTT message broker

test -x ${DAEMON} || exit 0

umask 022

. /lib/lsb/init-functions

# Are we running from init?
run_by_init() {
    ([ "$previous" ] && [ "$runlevel" ]) || [ "$runlevel" = S ]
}

export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"

case "$1" in
  start)
        if init_is_upstart; then
            exit 1
        fi
        log_daemon_msg "Starting network daemon:" "mosquitto"
        if start-stop-daemon --start --quiet --oknodo --background  --make-pidfile --pidfile ${PIDFILE} --exec ${DAEMON} -- -c /etc/mosquitto/mosquitto.conf ; then
            log_end_msg 0
        else
            log_end_msg 1
        fi
        ;;
  stop)
        if init_is_upstart; then
            exit 0
        fi
        log_daemon_msg "Stopping network daemon:" "mosquitto"
        if start-stop-daemon --stop --quiet --oknodo --pidfile ${PIDFILE}; then
            log_end_msg 0
            rm -f ${PIDFILE}
        else
            log_end_msg 1
        fi
        ;;


  reload|force-reload)
        if init_is_upstart; then
            exit 1
        fi
        log_daemon_msg "Reloading network daemon configuration:" "mosquitto"
        if start-stop-daemon --stop --signal HUP --quiet --oknodo --pidfile $PIDFILE; then
            log_end_msg 0
        else
            log_end_msg 1
        fi
        ;;

  restart)
        if init_is_upstart; then
            exit 1
        fi
        log_daemon_msg "Restarting network daemon:" "mosquitto"
        if start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile ${PIDFILE}; then
            rm -f ${PIDFILE}
        fi
        if start-stop-daemon --start --quiet --oknodo --background --make-pidfile --pidfile ${PIDFILE} --exec ${DAEMON} -- -c /etc/mosquitto/mosquitto.conf ; then
            log_end_msg 0
        else
            log_end_msg 1
        fi
        ;;

  try-restart)
        if init_is_upstart; then
            exit 1
        fi
        log_daemon_msg "Restarting Mosquitto message broker" "mosquitto"
        set +e
        start-stop-daemon --stop --quiet --retry 30 --pidfile ${PIDFILE}
        RET="$?"
        set -e
        case $RET in
            0)
                # old daemon stopped
                rm -f ${PIDFILE}
                if start-stop-daemon --start --quiet --oknodo --background --make-pidfile --pidfile ${PIDFILE} --exec ${DAEMON} -- -c /etc/mosquitto/mosquitto.conf ; then
                    log_end_msg 0
                else
                    log_end_msg 1
                fi
                ;;
            1)
                # daemon not running
                log_progress_msg "(not running)"
                log_end_msg 0
                ;;
            *)
                # failed to stop
                log_progress_msg "(failed to stop)"
                log_end_msg 1
                ;;
        esac
        ;;

  status)
        if init_is_upstart; then
            exit 1
        fi
        status_of_proc -p ${PIDFILE} ${DAEMON} mosquitto && exit 0 || exit $?
        ;;

  *)
        log_action_msg "Usage: /etc/init.d/mosquitto {start|stop|reload|force-reload|restart|try-restart|status}"
        exit 1
esac

exit 0

just as a guess… Maybe /lib/lsb/init-functions has set init_is_upstart to true (should be false as ubuntu 16.04 should use systemd)?

Where do I set it init_is_upstart to false?
I did find this in /lib/lsb/init-functions file:

# If the currently running init daemon is upstart, return zero; if the
# calling init script belongs to a package which also provides a native
# upstart job, it should generally exit non-zero in this case.
init_is_upstart()
{
   if [ -x /sbin/initctl ] && /sbin/initctl version 2>/dev/null | /bin/grep -q upstart; then
       return 0
   fi
   return 1
}

as a first test, you could remove the test part:

if init_is_upstart; then
    exit 1
fi

(at least from start and stop block) to evaluate if this solves your problem. It is best practice to make a backup of the file before change it :wink:
If mosquitto starts without errors, you should investigate why the function init_is_upstart does return the wrong value. I would strongly recommend not to change the function itself, because there are many scripts which rely on the function.

Udo,
I don’t really know Ubuntu that good and I am not sure I want to risk messing up my OpenHAB system by making all these changes. I think I’ll stay with my current setup. I appreciate you trying to help me.
Thanks.