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
|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.
Every job consists of six fields. The fields are:
When all the first five fields match the current data and time, the command in the sixth field is executed.
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
The first five fields following the below mentioned rules:
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
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.
0 9 * * 1-5 touch Linux