Mosquitto: Service will not start without existing /var/log/mosquitto folder (tmpfs /var/log)

Created on 17 May 2018  ยท  4Comments  ยท  Source: eclipse/mosquitto

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).

not a bug

Most helpful comment

FWIW, a workaround is to add the lines marked with +:

  • For sysv-init, in /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
[...]
  • For systemd, in /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
[...]

All 4 comments

I'd like to clarify your intent.

  • It is a bug to be resolved?
  • It is not a bug but you want submit the improvement.
  • You are just asking the workaround more elegantly.

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 +:

  • For sysv-init, in /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
[...]
  • For systemd, in /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
[...]
Was this page helpful?
0 / 5 - 0 ratings

Related issues

HenryNe picture HenryNe  ยท  5Comments

CodingSpiderFox picture CodingSpiderFox  ยท  3Comments

ibrahimkoujar picture ibrahimkoujar  ยท  7Comments

OkenKhuman picture OkenKhuman  ยท  3Comments

chipengliu picture chipengliu  ยท  5Comments