Easy Ways to Use Cron Jobs on Ubuntu 20.04

Cron Job is a task scheduler on Linux. Through this Cron Job you can schedule the execution of commands or scripts on Linux at specified times. This Cron Job is usually used to execute commands or scripts that are routine and repetitive, such as data backup, temp directory cleaning, etc.

This article will explain how easy it is to use Cron Job in Ubuntu 20.04. Here is the explanation:

1. Install Cron Job

$ sudo apt update
$ sudo apt install cron
$ sudo systemctl cron status

2. Edit Crontab
On Linux, the command to use a Cron Job is crontab .

$ crontab -e

3. Understanding Crontab Format Crontab
has a special format to determine when a command will be executed. Here is the format:

* * * * * <command to be executed>
– – – – –
| | | | |
| | | | —– Day (0 – 7) (Sunday = 0 or 7)
| | | ——- Months (1 – 12)
| | ——— Date (1 – 31)
| ———– Hours (0 – 23)
————- Minutes (0 – 59)

Each time column in the crontab can be populated in the following way:
* = all values ​​(any value)
, = separator between values ​​(value list separator)
– = range of values ​​(range of values)
/ = interval values ​​(step values)

4. Example Crontab
– Execute commands every minute of every day:

* * * * * <command to be executed>

– Execution of orders every hour (every 00 minutes) every day:

0 * * * * <command to be executed>

– Execution of orders every 15, 30, and 45 minutes daily:

15,30,45 * * * * <command to be executed>

– Execute commands every minute from minute 0 to minute 10 every day:

0-10 * * * * <command to be executed>

– Command execution every 30 minutes every day:

*/30 * * * * <command to be executed>

– Execution of orders every 00.00 every day:

0 0 * * * <command to be executed>

– Execution of orders every 17:00 every day:

0 17 * * * <command to be executed>

– Execution of orders every 1st at 06.00 :

0 6 1 * * <command to be executed>

– Execution of orders every 10th of December at 21.00:

0 21 10 12 * <command to be executed>

– Execution of orders every working day (Monday to Friday) at 16.00:

0 16 * * 1-5 <command to be executed>

– Execution of orders every holiday (Saturday to Sunday) at 08.00 :

0 8 * * 6-7 <command to be executed>

Note:
To ensure that the crontab you created is correct, you can perform a crontab simulation on the Crontab Guru site .

5. Run Shell Script on crontab :

Crontab can also be used to run shell scripts. The format is as follows:

* * * * * <full path script file>

Example:

0 0 * * * /home/user/test.sh

Make sure that the script file is executable:

$ chmod +x /home/user/test.sh

That’s an easy way to use Cron Job on Ubuntu 20.04. Thank you for visiting, I hope this article is useful.

Leave a Reply

Your email address will not be published.