Propeer way to log exit of an eternal loop script?

Bo Berglund bo.berglund at gmail.com
Sun May 7 13:51:00 UTC 2023


I have a script that logs MQTT telegrams to file in an infinite loop started
from crontab on boot, the basic lines following variable definitions are:


CMD="mosquitto_sub -h 192.168.xxx.yyy -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"
eval "$CMD" #This command runs until killed or a system reboot
#I want these lines to be executed when the system kills the script
NOW=$(date "+%F %T")
eval "echo \"${NOW} ---- Stop logging ----\" >> $FULLOG"



As it is now I do *not* get the stop message logged to file when the logging
process is killed manually or when the system is rebooted.

How can I modify the script so that this will happen?
I have googled and found that there is a "trap" command but I don't understand
how to use it in this situation...


-- 
Bo Berglund
Developer in Sweden




More information about the ubuntu-users mailing list