Table of Contents
Cron is a Linux utility that allows users to schedule a command or script to run periodically at a specified date, time, or intervals.
It is one of Linux’s most useful tools as cron jobs will save you the time and energy of doing repetitive tasks like system admin jobs like backup or cleaning directories, and more.
Creating a cron job is easier if you know and understand its syntax which has 2 formats:
- A B C D E USERNAME /path/to/command/ arg1 arg2
- A B C D E USERNAME /root/backup.sh
Now, what is the significance of these characters?
- A refers to minutes 0 – 59
- B refers to hours ranging from 0 – 23
- C represents days from 0 – 31
- D stands for months from 0 – 12
- E is for the days of the week; Monday which is 0 and 7 for Sunday
- USERNAME; replace this with your own username
- Path/to/command refers to the name of the script or command you want to schedule
- Minute refers to the exact minute the command sequence executes
- Hour pertains to the hour of the day the command sequence executes
- Day refers to the exact day of the month the command sequence executes
- The month is for the month of the year the command sequence executes
- Weekday is the day of the week the command sequence executes
- A command is the complete sequence of commands to execute
Other symbols that you can use:
- An asterisk * is used to specify possible values for a field. Selecting an asterisk in the hour field means the task will be performed every hour.
- Comma , specifies a list of value
- Dash or hyphen – specifies range of values
- Separator or slash / specifies a step value that will be repeated over a certain interval.
Most cron jobs have 3 elements: the script, the command, and the action or output. The script must be executed while the command is one that executes the script, and the action refers to the output depending on what the script does.
How to create a cron job?
- First, login to cPanel and then go to the “Advanced” section, then click “Cron Jobs”
- Before proceeding, you will be prompted to enter an email address. Just make sure that you will be using a valid email address. Click the “Update Email” option if a change of email address is necessary. The purpose of this is you will be sent an email once the cron job is completed.
- Select “Add New Cron Job” and then go to “Common Settings”, click on the regularly used intervals from the dropdown menu or set the frequency of your cron job through the dropdown box next to each time unit.
- Enter the desired command in the “Command Field” section
- And then click “Add New Cron Job”
Once done, your cron job will then be a file or command at a certain time, date, and interval based on the settings you selected. But please understand that doing this requires a good knowledge about Linux command to create or run a cron job effectively.