How to set up Cron Job on Ubuntu 18.04
Setting up a Cron job on Ubuntu 18.04
CronJob is a Linux task scheduler that schedules tasks at specific times and schedules tasks to repeat after a specific time. In this tutorial, you will learn how to set up a Cron job on Ubuntu.
Before you start setting up a Cron job on Ubuntu 18.04. The server requires a non-root user account with sudo privileges.
Ubuntu 18.04 server comes with crontab pre-installed, so you don’t need to install it manually. If you are working on the desktop, enter the following command:
Update the package manager index by entering
sudo apt update
Run the following command to install crontab
sudo apt install cron
Open crontab with text editor
To open crontab in a text editor, enter the following command
After entering the above command, you will be prompted to select a text editor. If you are editing for the first time, select or select nano. When you select a text editor, the following window opens.
Set up crontab on Ubuntu
After editing the file, simply exit and save using
Ctrl+x And press
In the screenshot above, the meaning of the m h dom mon dow user header is as follows:
* m = the minute the cron job will run. (0-59) * h = an integer that determines the time to execute the task. (0-23) * dom = day on which the cron job will run. (1 to 31). * mon = month in which the cron job will run. (1 to 12) * day of week for dow = 0 to 6 (0 for Sunday). (0-6) * user = name of the user running cron. * command = Linux command to execute.
Asterisk (*) for crontab timing
Here, the asterisk (*) is widely used in cronjob settings. That is, if * is specified for m (minute), the command will be executed every minute.
Basic example of Cron job
Cron runs every minute
* * * * * [user] [command]
Cron running 10 minutes of time, such as 12:10, 01:10, 02:10
10 * * * * [user] [command]
Cron running 30 minutes of an hour, such as 12:30, 01:30, 02:30
30 * * * * [user] [command]
Cron executed every hour (when minute becomes zero)
0 * * * * [user] [command]
Kron running at midnight
0 0 * * * [user] [command]
Cron running at 8am
0 8 * * * [user] [command]
Cron to execute PHP scripts
* * * * * root /usr/bin/php /var/www/html/project/test.php
Advanced cron examples
Using commas in crontab: The following command is executed on January 15, March, July and December 15 at 8:45 am
45 8 15 Jan,Mar,Jul,Dec * [user] [command]
If you use the division operator in crontab: If the minute is divided by 10, the following command will be executed every 0, 10, 20, 30, 40, 50 minutes (if the minute is divisible by 10).
*/5 * * * * [user] [command]
Crontab Dashesh: The next crontab runs every month from the 15th to the 20th. Dashes represent ranges.
0 0 15-20 * *[user] [command]
Here, you learned how to set up a Cron job on Ubuntu 18.04. If you have any questions about this, please comment below.