I have a bash script (that I'm converting to perl) that runs in an infinite loop (while true; do
) to poll devices on our network and log their responses to a text file. With each iteration of the (while
) loop, the text file for that device is appended with its latest information.
I would like to have this script always run -- if it gets hung, crashes, or is no longer writing to the appropriate text files -- it should be restarted.
Following the advice posted in this StackOverflow question, I could write the following bash script:
until myserver; do
echo "Server 'myserver' crashed with exit code $?. Respawning.." >&2
sleep 1
done
where myserver
is the polling program. This would account for issues with the script unexpectedly crashing or hanging, presuming a non-zero exit code were issued in those cases. However, if the script doesn't completely fail/exit, but fails in a way that just stops it from writing out to the text files -- I'd like to restart the script in this case as well. This is where a watchdog-like script would come in. I could use Python's watchdog
and write a script that uses Observer
library to monitor the text files being generated, like in this example. I would trigger on stagnant text files to issue a non-zero exit for the python script and then augment the above bash script as follows:
until [myserver -o pythonMon]; do
echo "Server 'myserver' crashed with exit code $?. Respawning.." >&2
sleep 1
done
where pythonMon
is the python script monitoring whether or not the text files are updating properly. Unfortunately, this approach requires three scripts (the main polling script and two monitoring scripts); it's a bit of a kludge . I'm looking to optimize/simplify this approach. Any recommendations? Ideally, I'd have a single script (at least a single monitoring script) to keep the polling script running versus two. Would there be a way to add file monitoring directly into the bash or perl code?
This is running on 64-bit, CentOS 6.5