I have made (with a Stack Exchange help) a script that checks if the web application is available and restarts the web app daemon in order to get it running again (usually helps until I'm at work). It's supposed to run every hour.
I've added the following line to sudo crontab -e:
0 * * * * bash /usr/local/bin/test.sh
The contents of test.sh:
#!/bin/sh
HTTP_STATUS=$(lynx -head -source https://example.com |head -1 |awk '{print $2}')
calendar=$(date "+%d-%m-%Y - %H:%M")
LOG="Log is placed in /usr/local/bin/test.log"
if [ "$HTTP_STATUS" = "503" ]
then
systemctl restart mydaemon
echo "$calendar - site unavailable ($HTTP_STATUS). Daemon restarted." >> test.log
elif [ "$HTTP_STATUS" = "200" ]
then
echo "$calendar - site available ($HTTP_STATUS)" >> test.log
else
echo "$calendar - site status unknown ($HTTP_STATUS)" >> test.log
fi
I have two other scripts in scheduled in cron that work just fine, only that they run every midnight. All of my scripts have same permissions and same owner.
My test.log file is not updated every hour so I suppose the script doesn't run. Running the script manually creates log entries.