Contents

Auto restart Weewx if SubState=exited

Contents

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#!/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 "systemctl status weewx" has "SubState=exited" then restart the service
if [ "$status" == "SubState=exited" ]; then
  sudo systemctl restart $service_name
  echo "$message" >> /var/log/weewx_restart.log
fi