Cron is a program that acts as a job scheduler to run different programs or scripts at set times or intervals.
Its main configuration file,
crontab, is a simple text file that contains the table of jobs to run where each line represents a new job to run.
To edit the crontab, simply type
It will then be opened in the system's default text editor - usually Vi.
Each line is divided into 6 sections each divided by a space. There may be spaces in the 6th section, but not 1 to 5. These first 5 sections represent the date/time values for when the job should run. The 6th section is the command to be run.
There are different values that can be used for time intervals.
The comma (',') allows for listing of values: e.g. 1,7,28,49
The hyphen ('-') allows for range of values: e.g. 5-9 will mean 5,6,7,8,9
The asterisk ('*') allows for all possible values: e.g.
every minute, or day etc.
The slash ('/') is interpreted as 'every': e.g /3 means
every 3 minutes (ie /1 is the same as *)
The 1st time digit represents the minutes (0-59)
The 2nd time digit represents the hour (0-23)
The 3rd time digit represents the day of month (1-31)
The 4th digit represents the month (1-12)
The 5th digit represents the day of week (0-6) [Sunday = 0 OR 7)
Example 1 - Here the tmp directory would be cleared at 1am every day:
Code:
0 1 * * * rm /tmp/*
Example 2 - Here the command will run every Monday at 2pm in March & July:
Code:
0 14 * 3,7 1 COMMAND TO BE RUN
Example 3 - Run command every 3 minutes:
Code:
/3 * * * * COMMAND TO BE RUN