Set Up Cron Job on Ubuntu 18.04
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 Ubuntu.
Before you start to set up Cron job on Ubuntu 18.04. You must have a non-root user account on your server with sudo privileges.
In Ubuntu 18.04 server crontab comes preinstalled so you don’t need to install it manually. If you are working on desktop please enter 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 text editor
To open crontab with text editor enter following command
Once you enter 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 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 midnioght
0 0 * * * [user][command]
Cron that runs at 8am
0 8 * * * [user][command]
Cron that runs 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:45am on 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 hour (when the minute is divisible by 10).
*/5 * * * * [user][command]
Dashesh in crontab:
The following crontab will run from 15th to 20th day of every month. As the dash represents range.
0 0 15-20 * *[user][command]
Here you have successfully learnt how to set up Cron job in Ubuntu 18.04. if you have any queries reagarding this, please comment below.