Tuesday, 28 February 2012

Crontab Examples

Cron is kinda like the at command only a hell of a lot better, but essentially it is used to running commands/scripts in the background at regular intervals.
To create or edit a crontab file add the following command into your shell: crontab -e This will bring up a screen much like the following:
Your commands will go at the bottom of this page. Crontab is a simple text file with a list of commands meant to be run at a certain time. The commands are controlled by Cron which executes them in the background. If you want to set a script to run on you system at regular intervals something like the below command should be added to the crontab file shown above:

 30 15 16 2 1 /scripts/test/runThisScript.sh

This doesn't seem to make much sense at first glance but let me try and explain. The first five numbers indicate at what intervals you wish to run your command/script, this is the breakdown

1 Minute 0-59
2 Hour 0-23 (0 = midnight)
3 Day 1-31
4 Month 1-12
5 Weekday 0-6 (0 = Sunday)
* = all

So we can see that I am asking Cron to run my script at 15:30 on the 16th February, Monday, plus every Monday in February. You could have it run everyday at 15:30 no matter what the date is:

30 15 * * * /scripts/test/runThisScript.sh

Make sure you give each a new line after each cron job, otherwise the job will not be executed...god knows why.. To save a cron-job press 'ctrl-K-X'. One of the jewels of Linux in my opinion.

more information can be found with 'man crontab' from your console.

No comments:

Post a Comment