Table of Contents
Set Up Cron Job on CentOS
CronJob is the task scheduler in Linux which schedules the task at a specific time or schedules task to repeat itself after a specific time. In this tutorial, you are going to learn how to set up a Cron Job on CentOS 8.
Before you start to set up a Cron job on CentOS 8. You must have a non-root user account on your server with sudo privileges.
Open crontab with a text editor
To open crontab with text editor enter following command
Once you enter the above command you will be asked to choose a text editor. Choose nano if you are first time editing or go with your choice.
In the crontab,
m h dom mon dow user header has the following meaning
* m = The minute when the cron job will run. (0 to 59)
* h = A integer determining the hour when the tasks will run. (0 to 23)
* dom = Day of the Month when the cron job will run. (1 to 31).
* mon = The month when the cron job will run. (1 to 12)
* dow = Day of the Week from 0-6 with Sunday at 0. (0 to 6)
* user = The User under which the cron will run.
* command = The Linux command you wish to execute.
Asterisks (*) on the crontab timings
Here in setting up cronjob asterisks (*) is used widely. What it means is that if you put * for m (minute) then it will run that command every minute.
Basic Examples of Cron Jobs
Cron that runs every minute
* * * * * [user] [command]
Cron that runs every 10 minutes
10 * * * * [user] [command]
Cron that runs every 30 minutes
30 * * * * [user] [command]
Cron that runs every hour (when the minute will become zero)
0 * * * * [user] [command]
Cron that runs at midnight
0 0 * * * [user] [command]
Cron that runs at 8 am
0 8 * * * [user] [command]
Cron that runs a PHP script
* * * * * root /usr/bin/php /var/www/html/project/test.php
Advanced examples of crontab
Using commas in crontab:
The following command will execute at 8:45 am on the 15th day of January, March, July, December.
45 8 15 Jan,Mar,Jul,Dec * [user] [command]
Using division operator in crontab:
As the minute is divided by 10. the following command will execute at every 0,10,20,30,40,50th minute of the hour (when the minute is divisible by 10).
*/10 * * * * [user] [command]
Dashes in crontab:
The following crontab will run from 15th to 20th day of every month. As the dash represents the range.
0 0 15-20 * *[user] [command]
Here you have successfully learned how to set up Cron job in CentOS 8. if you have any queries regarding this, please comment below.