Cron job is a task that can be run automatically at regular intervals. In this article, we will look how to run cron job every 5, 10, 15 minutes.
What is Cron Job?
Every Linux system allows you to set up cron jobs to be run at user-specific intervals. These intervals can be minutes, hours, days, weeks, months, day of the week, or a combination of these. Cron jobs are commonly used for administration and maintenance of linux system, databases or websites. These tasks can be performing back ups, checking for updates, sending emails, installing patches and so on.
How To Run Cron Job Every 5,10,15 Minutes
All cron jobs are listen in a text file called crontab in Linux. You can create, view, edit this file using crontab -e command.
Each cron job needs to be specified in a separate line of crontab file and must follow the following syntax. It consists of 6 space-separated fields, first 5 fields are time-based fields and the last field is the command(s) to be run.
* * * * * command(s) ^ ^ ^ ^ ^ | | | | | allowed values | | | | | ------- | | | | ----- Day of week (0 - 7) (Sunday=0 or 7) | | | ------- Month (1 - 12) | | --------- Day of month (1 - 31) | ----------- Hour (0 - 23) ------------- Minute (0 - 59)
The first five time-based fields accept the following operators.
* – asterisk means every occurrence. If it is placed in first field, it means every minute. If it is placed in second field, it means every hour, and so on.
– – hyphen allows you to specify range of values. For example, 1-5 in first field means the task will run every minute during first 5 minutes of every hour.
, – comma allows you to specify list of values. For example, specifying 15,30,45 in first field means that the task will run at minute 15, 30, 45 of every hour.
/ – slash allows you to specify interval steps. For example */10 in first field means the task will run every 10 minutes.
You can use the last 3 operators in conjunction such as 1-5,10,30-40/2 in the first field means that every hour, the task will run every minute during the first 5 minutes, then at minute 10, and then every 2 minutes starting from minute 30 till minute 40.
The command used in a cron job can be a shell script or a linux command.
Here are couple of examples of cron jobs
0 0 1 * * /home/user/backup.sh 0 10 * * * service apache2 restart
Also read : How to Use rsync command in Linux
Run Cron Job Every 5 Minutes
Here is the simple command to run cron job every 5 minutes.
*/5 * * * * command
The first field describes that cron job needs to run in steps of 5 minutes. The next fields have asterisk to specify that task needs to run every hour, day, week and month.
Run Cron Job Every 10 Minutes
Similarly, here is the simple command to run cron job every 10 minutes.
*/10 * * * * command
Run Cron Job Every 15 Minutes
Similarly, here is the simple command to run cron job every 15 minutes.
*/15 * * * * command
Run Cron Job Every hour
Similarly, here is the simple command to run cron job every hour at 0 minutes (e,g 1:00, 2:00, 3:00…).
0 * * * * command
If you want to run it at a different time of the hour (e.g 1:05, 2:05, 3:05…) then change the first field to 5
* * * * command