Continuing the discussion from Mosquitto won't start on boot after raspbian and emonsd update:
This issue has been in the back of my mind for a while, so I did a little more digging. I do wonder if it is in some way related to some of the other issues that have been seen with MQTT.
In any case, @jsroques came up with a solution for the mosquitto service, that is create a unit file that checks for the presence of the log file location before starting.
[Unit]
Description=Mosquitto MQTT Broker
Documentation=man:mosquitto(8) https://mosquitto.org/
[Service]
ExecStartPre=-/bin/mkdir -p /var/log/mosquitto
ExecStartPre=/bin/chown -R mosquitto:mosquitto /var/log/mosquitto
ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
Restart=always
[Install]
WantedBy=multi-user.target
The issue raised with this, was the undesirable nature of modifying stock files.
A little more research for me; (every day a learning day) reveals that unit files for systemd
can be placed in one of 3 places and each has a different precedence. Files in /etc
followed by /run
followed by /lib
are used in that order. You can also add in snippets, but I’d suggest a complete unit file is the better solution. reference.
Therefore, by putting this replacement service file in /etc/systemd/system
, systemd
will use that over the stock file in /lib/systemd/system/
However, I am surprised that the stock mosquitto service file does not include a After=syslog.target
directive as that may well prevent the issues (rather than check for and create the file paths) - just a thought.
The other related issue the above reference exposes, is relationship of the mqtt_input.service
to the mosquiito.service
. Implicitly, the mqtt
service requires the mosquitto
service so an After=:
directive has been included (though the syntax should be .target
rather than .service
I believe). However, it seems this directive does not implicitly mean it waits until the mosquitto
service has started as there is no actual dependency.
To ensure that dependency I think there is also a requirement for a Requires=mosquitto
directive in the mqtt_input
service file. Again though, I think this still allows systemd
to start the 2 in parallel. To prevent that, I suggest a Before=mqtt_input.target
would be required in the new mosquitto.service
unit file.
A note of caution; whilst this may be fine for the emonpi, self installs might, for instance, not run the mosquitto broker on the same machine as the emoncms instance. Adding in a stricter dependency requirement, might just break the mqtt_input
service for them.
On my part, this is all theory as I do not have an emonpi to test it on!