Sunday, August 16, 2015

self-healing cron job

I have been working on some automation. I recently broke it. At least the first iterations broke. Within a couple of minutes, retries do work. Now I should debug and figure out why. But for now my head hurts, I simply want it to work - even the "first time" the cron daemon attempts to run it, even if delayed.

Here follows the magic.

As an example the cronjob entry is something similar to:
# run every 30 minutes at top and bottom of every hour of every day
0,30 * * * * bash -l -c 'WittCron01' >> /dev/null 2>&1

cat WittCron01 
#!/bin/bash

# top priority cron job - kill any already running Automate scripts - start Automate

# kill any instances of Automate
pkill -f Automate

(( DISPLAY=:0 Automate 0<&- &>/dev/null &) &)

# give that instance of Automate some time to prove it can stay alive :)
sleep 10

# if Automate is still running after the above sleep 10
# then kill this and all instances of WittCron01
# else start a new instance of WittCron01
pgrep -f Automate && pkill -f WittCron01 || WittCron01


There you go.

No comments:

Post a Comment

This is Charles Weldon Witt. Thank you for commenting on my blog.