Friday, July 06, 2007

Setting up cron in Linux or Unix using shell script

Setting up cron is an easy way of performing time-scheduled jobs in linux or unix machines. Cyberciti has a good tutorial on how to setup cron.

Sometimes you may want to execute a script on 2nd Monday or 4th Thursday only. A one line shell script can do it for you.

Consider this:

$ date +%a

The output is: Fri
Now to execute a script on first Friday:

$crontab -e

Now, append the following code in script:

# Run a script called myscript.sh on First Friday at 11:30:

30 11 1-7 * Fri [ "$(date '+%a')" == "Fri" ] && /path/to/myscript.sh

No comments: