views:

262

answers:

3

I have a cron job that runs every hour. It accesses an xml feed. If the xml feed is unvailable (which seems to happen once a day or so) it creates a "failure" file. This "failure" file has some metadata in it and is erased at the next hour when the script runs again and the XML feed works again.

What I want is to make a 2nd cron job that runs a few minutes after the first one, looks into the directory for a "failure" file and, if it's there, retries the 1st cron job.

I know how to set up cron jobs, I just don't know how to make scripting conditional like that. Am I going about this in entirely the wrong way?

+1  A: 

Possibly. Maybe what you'd be better off doing is having the original script sleep and retry a (limited) number of times.

Sleep is a shell command and shells support looping so it could look something like:

for ((retry=0;retry<12;retry++)); do
    try the thing
    if [[ -e my_xlm_file ]]; then break; fi
    sleep 300 
    # five minutes later...
    done
MarkusQ
+1  A: 

As the command to run, try:

/bin/bash -c 'test -e failurefile && retrycommand -someflag -etc'

It runs retrycommand if failurefile exists

MattJ
I think this answers the question that OP was asking +1
Learning
wrapping everything in /bin/bash -c seems silly, since cron supports a SHELL variable.
ashawley
Thank you very much!
pg
A: 

Why not have your set your script touch a status file when it has successfully completed. Have it run every 5 minutes, and have the first check of the script be to see if the status file is less then 60 minutes old, and if it is young, then quit, if it is old, then fetch.

Zoredache
In some environments the "job" varies slightly every hour (e.g., pulling a timestamped dump) so this could get tangled.
MarkusQ