
CronJob is a task scheduler in Linux. It will schedule a task at a specific time or repeat automatically itself after a duration. Using this you can schedule scripts to be executed periodically. It is usually used for system admin jobs such as backups or cleaning temp directories and more. This tutorial will describe how to set up a Cron Job on Ubuntu system.
Prerequisites
Before you starting, you should have logged in with non-root user account with sudo privileges.
Install Crontab
By default, Ubuntu 18.04 server have pre-installed crontab so you do not need to install it again. For any reason you removed or you are working on desktop then you need to following below steps to install crontab.
First of all, you should update package manager index list by typing:
sudo apt update
Next, install crontab by executing the below command:
sudo apt install cron
You can verify installation by running below command:
sudo systemctl status cron
It should display output as below:
● cron.service - Regular background program processing daemon Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2019-05-27 03:44:33 UTC; 4 days ago Docs: man:cron(8) Main PID: 814 (cron) Tasks: 1 (limit: 1152) CGroup: /system.slice/cron.service └─814 /usr/sbin/cron -f
Edit crontab with text editor
Issue below command to open crontab with text editor:
crontab -e
If you are first time to run this command it might ask you to select text editor. You can choose editor as per your choice. It will show output as given below:
After making changes you need to use Ctrl+X and press y to quit and save.
Crontab lines
Each line has five time-and-date fields, followed by a command, followed by a newline character. The fields are separated by spaces.
The syntax of crontab entry should be as follow:
* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
Examples of Cron Jobs
Cron which will run at every minute:
* * * * /usr/bin/YourDirectory/YourCommand
To run cron at every 10 minutes:
10 * * * * /usr/bin/YourDirectory/YourCommand
Cron will run every hour (when the minute will become zero)
0 * * * * /usr/bin/YourDirectory/YourCommand
To set cron at midnight
0 0 * * * /usr/bin/YourDirectory/YourCommand
The cron should run at 7 AM
0 7 * * * /usr/bin/YourDirectory/YourCommand
Advanced examples of cron
Comma-separated values can be used to run more than one instance of a particular command within a time period. Dash-separated values can be used to run a command continuously.
To execute command at 6:30 am on 10th days of month January, April, July and October you can set as below:
30 6 10 Jan,Apr,Jul,Oct * /usr/bin/YourDirectory/YourCommand
The dash represents range so below crontab will run from 21st to 25th day of every month:
0 0 21-25 * * /usr/bin/YourDirectory/YourCommand
You also can use division operator in crontab. When you have requirement such as run cron after every 10th minute then you can use as below:
*/5 * * * * /usr/bin/YourDirectory/YourCommand
Conclusion
You have learned how to set up cron job on Ubuntu 18.04. If you want to learn more example you can get it here
If you have any query or suggestion you can leave comment below.
Leave a Reply