Mosquitto systemctl not starting

I installed via the all-in-one installer. I was under the impression that mosquitto would start at boot but it hasn’t been. I ran the systemctl status and got this:

pi@homeassistant:~ $ sudo systemctl status mosquitto.service -l
● mosquitto.service - Mosquitto MQTT Broker daemon
   Loaded: loaded (/etc/systemd/system/mosquitto.service; enabled)
   Active: activating (auto-restart) (Result: exit-code) since Wed 2017-01-04 23:08:58 PST; 250ms ago
  Process: 1688 ExecStart=/usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf -d (code=exited, status=3)

Jan 04 23:08:58 homeassistant systemd[1]: Failed to start Mosquitto MQTT Broker daemon.
Jan 04 23:08:58 homeassistant systemd[1]: Unit mosquitto.service entered failed state.
Jan 04 23:08:58 homeassistant mosquitto[1688]: Error found at /etc/mosquitto/mosquitto.conf:19.
Jan 04 23:08:58 homeassistant mosquitto[1688]: Error: Unable to open configuration file.

I looked at line 19 in my config file and it is
log_dest /var/log/mosquitto/mosquitto.log

I commented it out and then I get this:

pi@homeassistant:~ $ sudo systemctl start mosquitto.service
Job for mosquitto.service failed. See 'systemctl status mosquitto.service' and 'journalctl -xn' for details.
pi@homeassistant:~ $ sudo systemctl status mosquitto.service -l
● mosquitto.service - Mosquitto MQTT Broker daemon
   Loaded: loaded (/etc/systemd/system/mosquitto.service; enabled)
   Active: activating (auto-restart) (Result: resources) since Wed 2017-01-04 23:11:56 PST; 1s ago
  Process: 1979 ExecStart=/usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf -d (code=exited, status=0/SUCCESS)

Jan 04 23:11:56 homeassistant systemd[1]: Unit mosquitto.service entered failed state.

Here is my complete uncommented config:

allow_anonymous false
password_file /etc/mosquitto/pwfile
port 1883
listener 9001
persistence true
persistence_file mosquitto.db
persistence_location /var/lib/mosquitto/
pid_file /var/run/mosquitto.pid
protocol websockets
#log_dest /var/log/mosquitto/mosquitto.log
user mosquito

Any ideas? A permission problem perhaps?

I’m not sure its the problem, but this line only has one t. Reading the man page, it should be the same as in the password_file, so I would check that is the case.

i had similar, it was due to root owning /var/run, i’ll look what i did when im home to avoid this happening.

That’s weird, somehow autocorrect must have changed that when I copy/pasted it here from my text editor. it is “mosquitto” in my config file. Thanks for the suggestion!

When I launch mosquitto from the command line it works as expected. It just won’t auto-start with systemctl.

One other thing I noticed: there was no log file or even ‘mosquitto’ folder created at the designated place,
/var/log/mosquitto/mosquitto.log

Do I need to define that I want logging enabled in the config for that to happen?

Also no pid file exists at:
/var/run/mosquitto.pid

Not sure if that is supposed to be created automatically but it’s not happening.

What command do you run when you do this?

@gpbenton I just type ‘mosquitto’ from the prompt and mosquitto runs. It works for my Smartthings-MQTT-Bridge with Home Assistant.

I just checked the ownership of the files in /etc/mosquitto:

pi@homeassistant:/etc/mosquitto $ ls -l
total 92
-rw-r--r-- 1 root      root        230 Nov 22 14:47 aclfile.example
-rwxrwxrwx 1 pi        pi        37019 Jan  5 07:37 mosquitto.conf
-rw-r--r-- 1 root      root      36806 Nov 22 14:47 mosquitto.conf.example
-rw-r--r-- 1 root      root         23 Nov 22 14:47 pskfile.example
-rw------- 1 mosquitto mosquitto    13 Nov 23 14:48 pwfile
-rw-r--r-- 1 root      root        355 Nov 22 14:47 pwfile.example

Could be a problem here but I don’t know what.

also, when I do launch mosquitto from the prompt that way, it tells my its using the default configuration. I don’t know if that means it’s not using my config file?

pi@homeassistant:~ $ mosquitto
1483630748: mosquitto version 1.4.9 (build date 2016-11-22 14:44:16-0800) starting
1483630748: Using default config.
1483630748: Opening ipv4 listen socket on port 1883.
1483630748: Opening ipv6 listen socket on port 1883.

By default, mosquitto doesn’t read the config file, so its probably something in your config file that is stopping it working. I suggest specifying a different config file and adding each line until it stops.

1 Like

You could run:

mosquitto -c /path/to/your/config

And see what errors come up.

1 Like

Thanks for that tip. I found there is an error opening the pwfile. I see that the file is owned by user “mosquitto” while everything else in the directory is owned by “pi” or “root”

pi@homeassistant:~ $ mosquitto -c /etc/mosquitto/mosquitto.conf
1483734032: mosquitto version 1.4.9 (build date 2016-11-22 14:44:16-0800) starting
1483734032: Config loaded from /etc/mosquitto/mosquitto.conf.
1483734032: Error: Unable to open pwfile "/etc/mosquitto/pwfile".
1483734032: Error opening password file "/etc/mosquitto/pwfile".

pi@homeassistant:/etc/mosquitto $ ls -l
total 92
-rw-r--r-- 1 root      root        230 Nov 22 14:47 aclfile.example
-rwxrwxrwx 1 pi        pi        37019 Jan  5 07:37 mosquitto.conf
-rw-r--r-- 1 root      root      36806 Nov 22 14:47 mosquitto.conf.example
-rw-r--r-- 1 root      root         23 Nov 22 14:47 pskfile.example
-rw------- 1 mosquitto mosquitto    13 Nov 23 14:48 pwfile
-rw-r--r-- 1 root      root        355 Nov 22 14:47 pwfile.example

I assume I need to change this. does this need to be a root file or owned by ‘pi’? How about the permissions?

EDIT- I fixed the password issue

now I get a new error:

pi@homeassistant:~ $ mosquitto -c /etc/mosquitto/mosquitto.conf
1483736874: mosquitto version 1.4.9 (build date 2016-11-22 14:44:16-0800) starting
1483736874: Config loaded from /etc/mosquitto/mosquitto.conf.
1483736874: Opening websockets listen socket on port 9001.
1483736874: Error: Unable to create websockets listener on port 9001.

apologies for not coming back when i said i would, i had the same problem, the daemon wont start unless it can write a .pid

if i look at my config i have the following in 3x files;

#cat /etc/init/mosquitto.conf

description “Mosquitto MQTTv3.1 broker”
author “Roger Light <[email protected]

start on net-device-up

respawn

exec /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

#cat /etc/mosquitto/mosquitto.conf

pid_file /var/run/mosquitto/mosquitto.pid
listener 1883
listener 8883
user mosquitto
persistence true
persistence_location /var/lib/mosquitto/
persistence_file mosquitto.db
password_file /etc/mosquitto/pwfile

#cat /etc/systemd/system/mosquitto.service

[Unit]
Description=Mosquitto MQTT Broker daemon
ConditionPathExists=/etc/mosquitto/mosquitto.conf
Requires=network.target

[Service]
Type=simple
ExecStartPre=/bin/sh -c ‘/usr/bin/test -d /var/run/mosquitto || /bin/mkdir /var/run/mosquitto || /bin/chown mosquitto:mosquitto /var/run/mosquitto || /bin/rm -f /var/run/mosquitto/mosquitto.pid’
ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf -d
ExecReload=/bin/kill -HUP $MAINPID
PIDFile=/var/run/mosquitto/mosquitto.pid
Restart=on-failure

[Install]
WantedBy=multi-user.target

hope this helps

1 Like

That would imply that something is probably already listening on 9001. You can see that through netstat (although not sure what all is included on pi as I use an ubuntu box).

Is there a reason you aren’t using 1883 (unencrypted) or 8883 (encrypted)? Those are standard MQTT ports.

Also, as I am sure you figured out from your comment above, whatever user you start mosquitto under needs read access. You would have to do a "sudo -u " if you wanted to test it under the mosquitto user to see how it really runs instead of trying to just do “mosquitto -c …”.

Actually, the pwfile has those permissions because mosquitto is configured to be run as the user mosquitto, and only that user should have persmission to read the pwfile - as a security precaution.

As @mstberto said above, you can use sudo -u mosquitto … to run the program as the mosquitto user, which will be the final goal.

Hey guys, thanks a bunch for all the replies. I managed to get it working! I’m still not sure what the original problem was but after I deleted the password file and created a new one it worked.

The reason the port was busy when I checked it with mosquitto -c /etc/mosquitto/mosquitto.conf was because my fix allowed the systemctl to actually launch when I rebooted but I didn’t realize it.

I set it to 9001 for a reason I don’t remember…probably just some tutorial I was following or because of some conflict with another service. Oh well, it works fine now! Cheers

@hoffsta how did you create a new password file? I am getting the same errors as you did. Can you please help?

Thanks.

follow these commands

sudo su mosquitto
rm /etc/mosquitto/pwfile
touch /etc/mosquitto/pwfile
exit

I just wanted to thank everyone is this topic to also help me resolve my issue of mosquitto no longer working.

Running mosquitto -c /etc/mosquitto/mosquitto.conf gave me a first clue and there the problem was that my /var/log was 100% full.

After resolving that issue, the problem became that mosquitto would start on port 1883 but immediately shutdown again. The mosquitto.log indicated a second attempt to start another instance on port 1883. After some research, it seems something might have changed in version 1.5.

By commenting out the listener 1883 as suggested in the mosquitto.conf, everything is working again.

Hope this helps somebody in the future.

1 Like

Hi hoffsta, can you tell me how you fixed this issue?
I have the same problem and don’t know what to do.

Thanks in advance.