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?
Conditional crontab command in bash script
bash;cron;scheduling
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/bashif [ `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  fifiThe 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/bashif [ `arping -w 59 ...` -ne 1 ]; then  sleep 1079  if [ `arping -w 59 ...` -eq 1 ]; then    # do your processing here  fifiIf 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