The “cron” or the “cron job” is a time-based job used to automate any commands or scripts at the specified time interval. It is a software tool that schedules jobs periodically at a fixed time, day, date, or interval without user interference.
The cron jobs are used for executing recurring jobs such as system maintenance, cleaning temporary files, and scheduling backups. It seems to be like a task scheduler available in Windows OS. This post elaborates on the objective, functionality, and working of the cron jobs.
The content list of this post is specified below:
- How Does the Cron Jobs Work?
- How to Schedule a cron job?
- How to View all Cron Jobs?
- How to Delete the Cron Jobs?
Let’s start with the purpose of the cron job.
How Do the Cron Jobs Work?
The cron jobs are defined in the “Crontab” file, also known as the “cron-table” text file. Generally, it manages the execution of defined cron jobs systematically. The “Crontab” works on the “crond” daemon that executes in the background to check whether any scheduled jobs need to be executed.
To create the “Crontab” file, some necessary commands are available that are listed below in the table format:
|crontab -l||Lists down the crontab file contents i.e., shows the defined cron jobs|
|crontab -e||Edits the crontab file in the text editor.|
|crontab -u||Specifies the particular user to edit the crontab file.|
|crontab -r||Deletes/Removes the crontab file, including all cronjobs defined.|
|crontab -v||Shows the last modified time of the crontab file.|
The basic syntax of crontab for all the cron jobs is defined below:
* * * * * command
The syntax is divided into two main parts that are described below:
- “Five asterisks”: Specifies the time interval that is further divided into five fields:
- First (*): Identifies the “minutes(0-59)”
- Second (*): Denotes the “hours(0-23)”
- Third (*): Shows the “days of the month(1-31)”
- Fourth (*): Represents the “month(1-12)”
- Fifth (*): Tells the “day of the week(0-7)”
- command: Indicates the “command” that needs be executed at the specified time interval.
As you can see in the above syntax, there are few characters or operators in the first five-time fields to specify multiple values are discussed in the below table:
|*(asterisk)||Specifies all the possible values for a defined field.|
|-(hyphen)||Supports a range/sequence of values.|
|,(Comma)||Shows a list of values.|
|/(Slash)||Identifies a step of values|
How to Schedule Cron Jobs?
Follow the step-by-step instructions to schedule the cron jobs at the defined time sequence.
Step 1: Verify the Cron Service Status
Before scheduling a new/old cron job, first open the terminal(Ctrl+Alt+T) and check the “cron service“. To perform this task executes the “systemctl status” command in this way:
$ sudo systemctl status cron
In the output, the highlighted green line verifies that the crontab service is in an “active (running)” state. Enter the “q” tab to exit it from the console.
Note: Start the Crontab Service (If Inactive)
In some cases, the “cron service” is not in an active state and displays the “inactive” status as shown below:
$ sudo systemctl status cron
Use the “systemctl start” command to start the “cron service” for scheduling the cron job:
$ sudo systemctl start cron
Step 2: Create a Crontab File
Next step is create the crontab file because it is not available by default. To do so, execute the following “crontab” command:
$ crontab -e
If the user is creating it for the first time, then it will ask to select the text editor. It is recommended to select the easiest “/bin/nano” text editor by passing the “1” integer value in the terminal.
After that, It will create a new crontab “/tmp/crontab.qVMFOS/crontab ” as shown in the below image:
Now, in this file, the user can add all the desired cron jobs that will run at the particular time as per requirements.
Example 1: Run a Cron Job Every Minute
The user can easily execute the task as a cron job every 5 minutes. For this purpose, the generalized syntax is written below:
* * * * * command
Navigate to the end of the “crontab” file and add the following command as an example:
* * * * * echo "Welcome to Linux at $(date)" >> $HOME/New.txt
When the user “itslinuxfoss” will read the “New.txt” file then it will automatically show the specified message every minute:
Note: Click here to read our detailed article Run Cron Jobs Every 5, 10, or 15 Minutes on scheduling a cron job at specific minutes.
Example 2: Run a cron Job Every Hour
Add the below-mentioned line to the newly created crontab file to schedule the “Sample.sh” script located in the “test” directory as a crontab job:
0 * * * * /home/itslinuxfoss/test/Sample.sh
The above line is divided into two major parts:
- “0 * * * *”: Parameter’ refers to the cron daemon for the execution of “Sample.sh” cron job for every hour.
- path: Identifies the absolute path of the “Sample.sh” script.
Example 3: Run Multiple Cron Jobs at the Same Time
There are two possible ways to run multiple commands as the same cron job one is AND “&&” operator, and the second is the “;(Semi-Colon)” symbol.
The “&&” operator runs the next command when the first command executes successfully. However, the “;” colon runs each command independently.
Syntax Using “&&” Operator:
$ * * * * * /path/to/command_1 && /path/to/command_2 && ...../path/to/command_N
Syntax Using “;” Symbol:
$ * * * * * /path/to/command_1 ; /path/to/command_2 ; ..... /path/to/command_N
Note: Click here to read our detailed article Run Multiple Commands in the Same Cron Job to run multiple commands in the same cron job.
How to View all Cron Jobs?
Once the required cron jobs are defined, they can be easily viewed with the help of the “crontab” command followed bythe “-l(list)” argument:
$ crontab -l
The above command displays all the defined cron jobs in the terminal.
How to Delete the Cron Jobs?
The “crontab” offers the “-r” option to remove the crontab file completely from the system. Suppose to delete the above-created crontab file, use the “crontab” followed by the “-r(remove)” option:
$ crontab -r
The above command executed successfully without any error and has deleted the crontab file. For more verification use the following “ls” command in the terminal:
$ sudo ls /var/spool/cron/crontabs
The command did not return any crontab file that verifies that the created crontab file has been removed from the local system.
In Linux, the “cron” is the pre-installed software tool that runs the pre-defined processes in the crontab file at a defined time interval. These processes can be executed at a specific time, i.e., “minutes”, “days”, “days of the week”, “hours”, and “days of the month”. To specify this time sequence the crontab generalized syntax offers strings and operators. This post has briefly described the purpose, working, and usage of the cron job.