Auto restart Weewx if SubState=exited or SubState=failed"
I have a personal weather station server running on Weewx, which posts weather data to https://kvw.gr. I have a cron job that stops Weewx for 20 minutes before midnight to allow the Weatherlink IP interface to upload daily data to weatherlink.com. However, in some cases Weatherlink IP interface was busy posting to weatherlink.com during Weewx’s start-up phase, causing Weewx to be unable to connect and retrieve LOOP data. This results in Weewx staying in this state until I manually restart the service.
To resolve this issue, I have created a simple bash script that checks the running status of Weewx and automatically restarts it if it is in an “exited” status. This script is run every 30 minutes through a cron schedule.
#!/bin/bash
# crontab example running every 20 minutes: 0/20 * * * * /home/pi/restart_if_stopped__weewx.sh
# Define the name of the service
service_name="weewx"
current_time=$(date +%Y-%m-%d\ %T)
message="WEEWX was found stopped and restarted at: $current_time"
# Check the status of the service
status=$(systemctl show $service_name | grep "SubState")
# If the status is "exited" or "failed", restart the service
if [ "$status" == "SubState=exited" ] || [ "$status" == "SubState=failed" ]; then
systemctl stop $service_name
sleep 10
systemctl start $service_name
echo "$message" >> /var/log/weewx_restart.log
fi
Comments