How to persist MQTT logging across reboot?
Bo Berglund
bo.berglund at gmail.com
Wed Dec 21 17:57:38 UTC 2022
On Mon, 19 Dec 2022 11:51:47 +0100, Bo Berglund <bo.berglund at gmail.com> wrote:
Just for completeness in case someone else stumbles across this thread:
This details my solution:
>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.
I added this to my crontab:
#MQTT log all activity, start after 30s delay
@reboot screen -d -m /home/bosse/bin/logmqtt 30
Where the basic content of script logmqtt is this:
-----------------------
#!/bin/bash
MQTTID="UBU-$RANDOM" #Make mqtt identifier somewhat unique (IMPORTANT)
LOGDIR="/home/bosse/MQTT/log"
LOGFILE="aspo_mqtt.log"
NOW=$(date "+%F %T")
FULLOG="${LOGDIR}/${LOGFILE}"
CMD="mosquitto_sub -h 192.168.117.131 -i $MQTTID -F '@Y- at m-@d @H:@M:@S ; %t ;
%p' -t '#' >> ${FULLOG}"
cd "$LOGDIR"
NOW=$(date "+%F %T")
eval "echo \"${NOW} ---- Start logging ----\" >> $FULLOG" #Add start to log
eval "$CMD" # Run the mqtt logging operation command
-----------------------
This worked fine when I rebooted the Ubuntu server and the logging was restored.
>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:
Instead I added another item to crontab in order to archive the logfile contents
once per day (shortly after midnight):
#Rotate the aspo_mqtt.log file at midninght + 1 minute
1 0 * * * /home/bosse/bin/asporotatemqttlog > /dev/null 2>&1
Where the script asporotatemqttlog looks like this:
---------------------------------
#!/bin/bash
#Daily rotate of the mqtt log
LOGPATH="/home/bosse/MQTT/log/"
LOGDAY=`date --date="yesterday" +%Y%m%d`
OLDLOG="${LOGPATH}aspo_mqtt.log"
NEWLOG="${LOGPATH}aspo_mqtt_${LOGDAY}.log"
if [ -e $NEWLOG ]; then #New log exists, rotation done
echo "Rotated log exists: $NEWLOG"
echo "Log rotation already done!"
exit
fi
#Here only if new log is missing i.e. rotation has not been done
if [ -e $OLDLOG ]; then
NOW=`date "+%Y-%m-%d_%H:%M:%S"`
CMD="cp $OLDLOG $NEWLOG ; echo \"$NOW Log rotated ------- \" > $OLDLOG"
eval "$CMD"
else #Old log is nonexisting!
echo "No log to rotate!"
exit
fi
-----------------------------------
Notice that now I don't move the existing logfile to the new name but instead I
copy it and then I clear its content so additional logging winds up in a near
empty same file.
--
Bo Berglund
Developer in Sweden
More information about the ubuntu-users
mailing list