I run mosquitto on my raspberry pi with /var/log/ as a tmpfs folder; I did this by adding the following line to my /etc/fstab file:
tmpfs /var/log tmpfs defaults,noatime,mode=0755 0 0
After a reboot, the mosquitto service won't restart until I manually create the folder /var/log/mosquitto.
Also, it doesn't give any relevant warning message to this issue when I use the systemctl command as stated below, only that is has been started:
[email protected]:/var/log
#> systemctl status mosquitto.service
โ mosquitto.service - LSB: mosquitto MQTT v3.1 message broker
Loaded: loaded (/etc/init.d/mosquitto; generated; vendor preset: enabled)
Active: active (exited) since Thu 2018-05-17 15:20:26 CEST; 1h 29min ago
Docs: man:systemd-sysv-generator(8)
Process: 331 ExecStart=/etc/init.d/mosquitto start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/mosquitto.service
May 17 15:20:26 pi.home systemd[1]: Starting LSB: mosquitto MQTT v3.1 message broker...
May 17 15:20:26 pi.home mosquitto[331]: Starting network daemon:: mosquitto.
May 17 15:20:26 pi.home systemd[1]: Started LSB: mosquitto MQTT v3.1 message broker.
[email protected]:/var/log
#>
Right now, I'm creating the folder after booting and restarting mosquitto, but I think a more elegant solution (in the service script itself) would be better. I'll see if I can come up with something later this week and send in a pull request, but I don't know if I know how to fix this in the most compatible way (with other distributions than just raspbian alone).
I'd like to clarify your intent.
A bugfix that would create the logging directory if it doesn't exist post-install (or at least show a decent error message in the logs somewhere without crashing) would be nice, as I've already found out that my fluency with the used programming language is far from being useful.
I don't think mosquitto should auto-create the logging directory. I tried another famous network software, and confirmed it behaved as same as mosquitto.
Regarding error message, you can look the message "Unable to open log file /nodir/mosquitto.log for writing." by running mosquitto on the shell.
FWIW, a workaround is to add the lines marked with +:
/etc/init.d/mosquitto:[...]
set -e
PIDFILE=/var/run/mosquitto.pid
DAEMON=/usr/sbin/mosquitto
+ LOGDIR=/var/log/mosquitto
# /etc/init.d/mosquitto: start and stop the mosquitto MQTT message broker
test -x ${DAEMON} || exit 0
+ test -d ${LOGDIR} || /bin/mkdir -p ${LOGDIR} && /bin/chown mosquitto:adm ${LOGDIR} && /bin/chmod 740 ${LOGDIR}
umask 022
[...]
/lib/sytemd/system/mosquitto.service and/or /etc/systemd/system/multi-user.target.wants/mosquitto.service (assuming user mosquittoand group adm for the log file):[...]
[Service]
Type=notify
NotifyAccess=main
ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
+ User=mosquitto
+ PermissionsStartOnly=true
+ ExecStartPre=-/bin/mkdir -p /var/log/mosquitto
+ ExecStartPre=-/bin/chown mosquitto:adm /var/log/mosquitto
+ ExecStartPre=-/bin/chmod 740 /var/log/mosquitto
[Install]
WantedBy=multi-user.target
[...]
Most helpful comment
FWIW, a workaround is to add the lines marked with +:
/etc/init.d/mosquitto:/lib/sytemd/system/mosquitto.serviceand/or/etc/systemd/system/multi-user.target.wants/mosquitto.service(assuming usermosquittoand groupadmfor the log file):