This script allows to log all MQTT messages transmitted by an MQTT Broker.
#!/bin/bash
sleep 10
service=mosquitto
BROKER_IP='192.168.86.53'
date=`/bin/date "+%Y.%m.%d.%H.%M.%S"`
echo "############################################" >> /mnt/samba/mqtt.txt
echo "# Raspberry Pi Rebooted #" >> /mnt/samba/mqtt.txt
echo "# $date # " >> /mnt/samba/mqtt.txt
echo "############################################" >> /mnt/samba/mqtt.txt
#echo "############################################" >> /mnt/samba/mqtt_face_reco.txt
#echo "# Raspberry Pi Rebooted #" >> /mnt/samba/mqtt_face_reco.txt
#echo "# $date # " >> /mnt/samba/mqtt_face_reco.txt
#echo "############################################" >> /mnt/samba/mqtt_face_reco.txt
# Sleep for 30s before checking if mosquitto has been started
sleep 30
if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))
then
echo "$service is running!!!" >> /mnt/samba/mqtt.txt
echo "$service is running!!!" >> /mnt/samba/mqtt_plex.txt
# echo "$service is running!!!" >> /mnt/samba/mqtt_face_reco.txt
# Sends the command to grab all the sonoff mosquitto messages and store them into mqtt.txt file
sudo mosquitto_sub -h $BROKER_IP -t "sonoffs" -v >> /mnt/samba/mqtt.txt &
sudo mosquitto_sub -h $BROKER_IP -t "plex" -v >> /mnt/samba/mqtt_plex.txt &
# Sends the command to grab all the face_reco mosquitto messages and store them into mqtt_face_reco.txt file
#sudo mosquitto_sub -h $BROKER_IP -t "face_reco" -v >> /mnt/samba/mqtt_face_reco.txt &
else
# Try to start the service
/etc/init.d/$service start
fi
exit 0