Table of Contents
Set Up Cron Job on Debian 9
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 Debian 9.
Before you start to set up a Cron job on Debian 9. You must have a non-root user account on your server with sudo privileges.
In Debian 9 server crontab comes pre-installed so you don’t need to install it manually. If you are working on desktop please enter the following command.
update package manager index by typing following
sudo apt update
Install crontab by executing the following command
sudo apt install cron
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. After choosing text editor following window will open.
In above screenshot
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 10th minute of the hour like 12:10, 01:10, 02:10, etc.
10 * * * * [user] [command]
Cron that runs 30th minute of the hour like 12:30, 01:30, 02:30, etc.
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 cron
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 a range.
0 0 15-20 * *[user] [command]
Here you have successfully learned how to set up Cron job in Debian 9. if you have any queries regarding this, please comment below.