How to persist MQTT logging across reboot?

Bo Berglund bo.berglund at gmail.com
Mon Dec 19 10:51:47 UTC 2022


I have an MQTT broker running on a Linux box (RaspberryPi) to handle a number of
IoT sensors output data.

On my Ubuntu 20.04.5 LTS Server (no GUI) I have installed mosquitto client to
use as a subscribed client to log all of the data sent by the IoT devices.

It is done using this command (a single line, newsreader wraps...):

mosquitto_sub -h 192.168.117.131 -i 'ubuntusrvX' -F '@Y- at m-@d @H:@M:@S ; %t ;
%p' -t '#' >> /home/bosse/log/iot_mqtt.log

The command subscribes to everything using the '#' topic specifier wildcard and
it formats the output to contain the timestamp, topic and message all on one
line and appends it to the logfile. This is working fine.

I have manually started screen and entered the command on the command line and
then disconnected from the screen session. Then it runs as intended and creates
the iot_mqtt.log file and writes incoming messages to it.

Now I have the following 2 problems:

1) How can I make this logging work across a server reboot?
-----------------------------------------------------------
My server is used for a lot of other things and it is being updated regularly by
me, but once in a while there is a login message that the server needs to be
rebooted.

Also sometimes (not so often) there is a power outage causing a forced reboot
when power returns.
So I need to set it up in a way that makes it resume logging if the server
reboots.

2) How to rotate the logfile?
-----------------------------
This log file fills up pretty quickly and after some time I would like to rotate
it, but when I tried this it did not work:

mv iot_mqtt.log iot_mqtt.log.1

I expected the file iot_mqtt.log to disappear and then reappear on the next
received mqtt message but instead what happened was that the iot_mqtt.log.1
received the message!

So now my command writes to a different file than what was given....

Is this a safe approach instead:

cp iot_mqtt.log iot_mqtt.log.1 ; echo "log has been rotated" > iot_mqtt.log

I.e. copy the logfile and then reset it to empty with just the message.
This will basically move the content of the logfile to the new file and then
erase it.

Is there some other Ubuntu way of doing this?


-- 
Bo Berglund
Developer in Sweden




More information about the ubuntu-users mailing list