How to setup Cron Job on Ubuntu 18.04
Table of Contents
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.
After editing file simply exit and save by using
Ctrl+x and pressing
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 name of 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: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 regarding this, please comment below.
How to install Nginx on Debian 9
How to Install PhpStorm on Manjaro 18
How to Install PostgreSQL on Ubuntu 18.04
How to Install Mozilla Firefox on Linux Mint 19
How to Disable SELinux on CentOS 8
How to Install WebStorm on Ubuntu 18.04
How to Install Google Chrome on CentOS 8
How to Add Swap Space on Debian 9
How to Add Swap Space on Linux Mint 19
How to Copy, Cut, Paste in Vim or Vi
How to Install Node.js with npm on Linux Mint 19
How to Install Apache Tomcat 9 on Debian 10