Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How do I invoke a crontab command so that I can schedule to run a script with a 20 minute delay based on some condition?

Edit: What if I wanted a command that schedules a script to be executed only as many times as the condition evaluated to true in the system? What are my options?

share|improve this question
add comment

3 Answers

up vote 5 down vote accepted

Put the logic code for testing your condition in the script itself, don't try and put it in (or associate it with) cron - complex logic is not what cron was designed for.

So, in your script you test the condition and, if it evaluates to true, your processing code runs. If it evaluates to false, exit the script cleanly.

Assuming that your 'conditions' change as a result of processing the script (e.g. watching a folder for incoming files that need processing and processing one file every 20m), then eventually your condition will evaluate to false all the time because all the work has been done.

From your comments it looks like you are monitoring the availability of some server.

I don't do a heck of a lot with bash but how about this:

#!/bin/bash

if [ `ps ax | grep $0 | wc -l` -le 3 ]; then     #1
  if [ `arping ...` -ne 1 ]; then                #2
    sleep 1200
    if [ `arping ...` -eq 1 ]; then              #3
      # do your processing here
    fi
  fi
fi

The first if statement (#1) makes sure that this is the only instance of this particular script that is running. If another script is (still) running we exit and don't do anything.

The second (#2) is your initial 'is host pingable' test. If it is not, then the script waits 1200s (20min) before testing again (#3).

So, if two pings -- 20 minutes apart -- show that your host has become reachable then your processing code will run.

If you want to simplify things a little, try this:

#!/bin/bash

if [ `arping -w 59 ...` -ne 1 ]; then
  sleep 1079
  if [ `arping -w 59 ...` -eq 1 ]; then
    # do your processing here
  fi
fi

If you impose an arping deadline of a little under 1 minute (-w 59) for your checks, and tweak the sleep amount, then you can pretty much guarantee that the two tests and the sleep in between will be completed within your 20 minute period, so there should be no overlap with adjacent periods and no need to check to see if another script is still running.

Either of the above scripts would, of course, be invoked via a static cron entry which runs every 20 minutes:

*/20 * * * * /path/to/script.sh
share|improve this answer
1  
The condition is to check if a particular hostname/IP address is ping-able. I am using arping in this context. If it returns 1, then the address is ping-able, if not I want the code to go and wait as a scheduled cron job. After 20 minutes I again want to ping the same address and only if the address is up, I want to run the scheduled cron job. Otherwise I want to schedule it again after a delay of 20 minutes. Is this do-able using cron and a bash script? –  Arpith Oct 5 '12 at 2:16
    
I might be totally wrong but I believe the second condition should be different from the first condition. Right? The second arping condition in the first script should have -ne rather than -eq –  Arpith Oct 7 '12 at 15:17
    
Apologies the confusion. Great answer. Thank you for your time. –  Arpith Oct 8 '12 at 17:19
    
@Arpith Yes, you are right. Fixed. –  Tim Oct 8 '12 at 17:28
add comment

You would have to build a bash script that checked for the conditional and assigned a sleep to it. Then schedule that bash script to run at a specific time using cron. Cron itself doesn't not have support for conditionals or 'run after a delay'.

share|improve this answer
add comment

Okay you updated the question to minutes now.

Run every 20 minutes = run at 0 / 20 / 40 / 60 (=0) minute of all hours,

The entry would be:

0,20,40 * * * * /path/to/some_command

And if you want to execute it for limited times, use a state file to store how many times you've launched that script.

share|improve this answer
    
Sorry. You're right. Made the appropriate changes in the question. –  Arpith Oct 4 '12 at 11:35
    
You have not said anything about the execution of the cron job based on the evaluation of a conditional statement. –  Arpith Oct 4 '12 at 13:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.