As a user if you want to schedule certain activities which should repeat themselves on their own at some time in the future i.e., you want to schedule recurring tasks then use the crontab command. In this post we will learn scheduling recurring tasks using crontab command
There are different ways to use the crontab command as shown in the table below
Command | Use |
crontab -e | edit the crontab file for jobs |
crontab -l | list all the jobs |
crontab -r | remove all jobs |
crontab <filename> | Replace the current jobs with the ones mentioned in the file <filename> |
Note: Every user can create his own crontab file only. But the root user can use the -u <username> option to manage jobs for any other user.
Job Format
Every job consists of six fields. The fields are:
- Minutes (0-59)
- Hours (0-23)
- Day of Month (1-31)
- Month (1-12)
- Day of week (0-7 : 0 and 7 both mean Sunday)
- Command
When all the first five fields match the current data and time, the command in the sixth field is executed.
Example on Scheduling recurring tasks using crontab command
To schedule a recurring task use the crontab -e command
An editor (either nano or vi) will open where you can specify the task as shown below
The above job means : The system date will saved in the date.txt file on 24th Jan, at 8:08PM every year
Note: Make sure that the cron service is installed and running in your system.
To install use
#apt-get install cron (for Debian based systems like Ubuntu) or #yum install cron (for Redhat, Fedora CentOS)
To check the status whether cron is running or not use
$service cron status
If the service is not running then use the command
#service cron start
Rules while writing the crontab job
The first five fields following the below mentioned rules:
- Don’t care or always – *
- E.g., * * 14 Jan * tar cf etc.tar /etc
- Take backup of /etc directory every minute on 14 Jan every year
- Range : x-y (x and y inclusive)
- E.g., * 5-14 14 1 * tar cf etc.tar /etc
- Take backup of /etc directory every minute between 5AM to 3PM on 14th Jan
- Lists: x,y
- E.g., * 5-14 * * Mon,Fri tar cf etc.tar /etc
- Take backup of /etc directory every minute between 5AM to 3PM on every Monday and Friday
- •*/x – means an interval of x
- E.g., */3 * * * * tar cf etc.tar /etc
- Will execute the command every 3 minutes
- A number to specify a number of minutes or hours or date etc.
- E.g., for weekdays, 0 means sunday, 1 means monday etc.)
- When the “Day of Month” and “Day of week” filed are both other than *, then the command will be executed when either of the two fields match.
- E.g., * * 15 * Tue tar cf etc.tar /etc
- Will execute the command on 15th of every month and on every Tuesday
Practice Questions on crontab
Q1. Execute the command /usr/local/bin/yearly_backup at exactly 10 a.m. on February 4th, every year
Ans: 0 10 4 2 * /usr/local/bin/yearly_backup
or
0 10 4 Feb * /usr/local/bin/yearly_backup •
Q2. Send an email containing the word Linux to the owner of this job, every three minutes between 9 a.m. and 4 p.m., on every Friday in June.
Q3. Interpret
0 9 * * 1-5 touch Linux