11

I am trying to run a Mosquitto MQTT Broker on Ubuntu 16.04. I have instructions from 14.04, but since 16.04 no longer uses startup, I can't figure out how to convert to systemd.

Here are the commands for 14.04:

description "Mosquitto MQTT broker"
start on net-device-up
respawn
exec /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

EDIT:

This is what I got to so far. When I reboot and run systemctl status mosquitto.service I show that mosquitto is loaded but not active:

[Unit]
Description=Mosquitto MQTT Broker

[Service]
WorkingDirectory=/usr/sbin/mosquitto
ExecStart=/etc/mosquitto/mosquitto.conf
Restart=always

[Install]
WantedBy=multi-user.target

EDIT 2:

Ran sudo systemctl enable mosquitto.service. Now the error exit code is:

Process: 1720 ExecStart=/etc/mosquitto/mosquitto.conf (code=exited, status=200/CHDIR)
9

Installing sudo apt install mosquitto on 16.04 adds the /etc/init.d/mosquitto script. systemd uses that script to generate /run/systemd/generator.late/mosquitto.service, and this is what it says:

# 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
Before=shutdown.target
After=remote-fs.target
After=systemd-journald-dev-log.socket
Conflicts=shutdown.target

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

Looking inside /etc/init.d/mosquitto tells us that

/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf is already run by default

So just put your config into /etc/mosquitto/conf.d (referenced by mosquitto.conf) and enjoy.


Being a regular systemd service, you can:

  • systemctl status mosquitto to see its current status
  • sudo systemctl (start|stop) mosquitto to change the status for the current session
  • sudo systemctl (enable|disable) mosquitto to change startup behavior (without affecting the current state)
4
  • How does that file tell you that mosquitto is being run with -c option? – Petrus Theron Dec 13 '18 at 9:45
  • @PetrusTheron that's what happens in the /etc/init.d/mosquitto file with the start argument. – Mihail Malostanidis Dec 13 '18 at 9:53
  • My config inside the /etc/mosquitto/conf.d dir seems to be ignored. How can I regenerate /run/systemd/generator.late/mosquitto.service? – chris Jun 25 '19 at 16:16
  • @chris maybe it's not referenced in /etc/mosquitto/mosquitto.conf? What's in there? – Mihail Malostanidis Sep 23 '19 at 1:03
0

Your unit file's ExecStart= and WorkingDirectory= lines are completely wrong. Are you trying to start a config file, using the executable file(!) as its working directory?

If I understand correctly, the proper unit file would be:

[Unit]
Description=Mosquitto MQTT Broker
After=network.target

[Service]
ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
Restart=always

[Install]
WantedBy=multi-user.target

Also, please don't conflate starting and enabling the unit. Read my similar answers about this:

0

Be sure that you have mosquitto running:

ps -aux | grep mosquitto

And you have this in default.conf:

listener 1883

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.