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?

3 Answers 3


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:


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

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:


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

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

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'.


Okay you updated the question to minutes now.

So you would 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.

  • Sorry. You're right. Made the appropriate changes in the question.
    – Arpith
    Oct 4, 2012 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, 2012 at 13:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .