What is crontab?
Crontab is a time-based job scheduler under Linux, it is used especially for maintenance and repetitive jobs that need to run at a certain time and date.
How does it work?
In fact the algorithm behind crontab is really simple :
- read the cron file ( /usr/etc/crontab )
- if there are commands to be run .. run them
- sleep one minute
- repeat the above steps
View and manage cron job entries
To view the current list of cron jobs you can use this command :
crontab -l
To delete the list of scheduled jobs :
crontab -r
To edit the jobs list you will need to have basic knowledge of the vi text editor (the default editor for crontab) and run this command :
crontab -e
Anatomy of a cron entry and operators
Every cron entry has the following structure :
* * * * * command
, the stars (*) means this :
- first one : minutes (0-59)
- second one : hours (0-23)
- third one : days (1-31)
- forth one : months (1-12)
- fifth one : day of the week (0-6, where 0 = Sunday)
And the operators :
- "*" - every unit (every minute, every hour ...)
- "," - separator for a list of values, ex : 1,2,7
- "-" - separator for a range of values, ex : 1-7 (means 1,2,3,4,5,6,7)
- "/" - modulo operator, ex : */5 every unit divisible by 5
Examples
Run a job every 5 minutes :
*/5 * * * * command
Run a job at 23:59 every day in February :
59 23 * 2 * command
Run a job every 12 hours every Monday from the first trimester :
0 0,12 * 1-3 1 command