Scheduling Tasks with the Cron Command on Linux

Learn via video courses
Topics Covered

Overview

Scheduling tasks is an essential aspect of any computing environment. In Linux, Cron is one of the most commonly used tools for scheduling tasks. Cron is a very powerful tool in Linux that is used to automate routine tasks by running scripts or commands at predefined time intervals. This article will provide an introduction to cron followed by what cron jobs are in Linux and providing examples of how to use cron job scheduling for scheduling tasks.

What is a Cron?

Suppose you have a Linux system and you want to daily backup your data at midnight. In that case, Linux provides you with a powerful tool called Cron that can be used for doing the job.

In Linux, Cron is a time-based job scheduler that runs in the background and is used to trigger specific commands or scripts at predefined intervals. Cron reads and executes commands from a special file called a crontab, which is a table of cron jobs. Cron can be used to schedule tasks that run every minute, hour, day, week, or month. cron job scheduling can be very useful for automating routine tasks such as backups, log rotations, system maintenance, etc.

What are Cron Jobs in Linux?

A cron job is particularly a command or script that is scheduled to run at specific time intervals using the cron daemon. The cron daemon reads the crontab file and executes the scheduled commands or scripts at the specified times. Cron jobs and cron job scheduling are used to automate routine tasks, such as backups, software updates, and system maintenance.

How to Add Cron Jobs in Linux

To add a new cron job in Linux, follow these steps:

  • Open a terminal window and log in as the user for whom you want to create the cron job.

Add Cron Jobs step1

  • Type the command crontab -e to open the user's crontab file in the edit mode in the default editor.

add-cron-Jobs-step2

  • Add the command or script you want to run to the crontab file, along with the schedule for when it should run.
  • Save and exit the crontab file.

Basic Crontab Syntax

We need to be aware of the fundamental components that make up this syntax to be able to configure cron job scheduling. A crontab command should be written in the following format:

The components of a cron command are as follows:

  • Cron Job Time Format
    The cron job's time/date and recurrence are specified in the first five fields of the command, * * * * *.
  • Directory/Command
    The /directory/command specifies the location or the script you want to run with the help of the Cron Job.
  • Output
    The output is optional segment. It specifies how the system will inform the user when a cron job is finished. By default, cron automatically sends a mail to the user when the job is being executed.

Cron Job Time Format

The Cron Job command's first five fields are the numbers that specify when and how frequently the command will run. Each position is separated by a space, which stands for a particular value.

Let's understand it better using the below table:

FieldValuesSyntaxDescription
1st *0-591 * * * *Cron Job is triggered everytime the system time has 1 on minutes hand
2nd *0-230 1 * * *Cron Job is triggered when system time is 1am
3rd *0-310 0 1 * *Cron Job is triggered every 1st day of month
4th *0-12, 0 = none and 12 = December0 0 0 1 *Cron Job is triggered in January only
5th *0-7, 0=7 = Sunday0 0 * * 1Cron Job is triggered on Mondays

Command to Execute

Next part of the syntax represents the command to execute, It is the exact directory/location of the file followed by the filename that we want to run in the cron Job.

This example describes that the file named script.sh is present in downloads directory which will be triggered with the cron job every midnight.

Output
When cron jon runs, it automatically sends an email to the crontab file's owner. This is the most common method of managing tasks and jobs. Keep in mind that routine tasks can quickly fill up your inbox.

By disabling the output email, which is an optional feature, you can stop that from happening. After the timing and command fields, add the string >/dev/null 2>&1 to stop email output.

Using Operators

Cron also provides operators that can be used to specify a range of values or intervals. The operators are:

  • Asterisk (*)
    Indicates all possible values. For example, * in the hour field would mean "every hour."
  • Comma (,)
    Used to specify multiple values. For example, 1,3,5 in the day of the week field would mean "on Monday, Wednesday, and Friday."
  • Dash (-)
    Used to specify a range of values. For example, 1-5 in the day of the week field would mean "from Monday to Friday."
  • Slash (/)
    Used to specify an interval. For example, */10 in the minute field would mean "every 10 minutes."

Cron job Examples

Now we know everything about cron job scheduling, so lets take some examples to understand it better. We will be using the above discussed syntax for adding cron jobs.

Example 1
Lets write a simple command for scheduling a file named script.sh.

Output
Breaking down each element of the above command:

  • 0 0 * * * is the schedule or timing part of this command, It specifies the time when the scipt.sh file will run. This means the script will run at 0th minute and 0th hour of every day, every month, and every day of the week (i.e., it will run once a day at midnight).
  • /downloads/script.sh is the command to be executed here. This specifies the location of the script (in this case, it is located in the /downloads directory) and the name of the script (script.sh), which will be run at the scheduled time.

Example 2
Lets utilize all the 5 *s in our command.

Output:
Breaking down each element of the above command:

  • 0 2 5 8 10 is the schedule or timing part of the above command, which specifies the time when the script will run. This means the script will run at 2:00 AM on the 5th, 8th, and 10th day of the month.
  • /downloads/script.sh is the file command to be executed here.

Lets take more examples utilizing the above syntax in the below table:

CommandDescription
* * * * * /downloads/script.shRun every minute
0 * * * * /downloads/script.shRun every hour
0 0 * * * /downloads/script.shRun every midnight
0 12 * * * /downloads/script.shRun everyday noon
0 0 1 * * /downloads/script.shRun every 1st of month
0 0 1 1 * /downloads/script.shRun every new year
0 0 * * 0 /downloads/script.shRuns every sunday midnight

Using Special Characters

We can also use special characters as we have discussed above

Example 3
We will be using ,(comma) separated values for providing multiple time format values.

Output

This cron job command schedules the execution of the script.sh script located in the /downloads directory at 12:00 AM, 12:10 AM, and 12:20 AM every day.

Example 4
We will be using -(dash) range values for providing range values.

Output

This cron job command schedules the execution of the script.sh script located in the /downloads directory every minute from 12:00 AM to 12:20 AM every day.

Example 5
We will be using /(slash) interval values for providing interval based timings.

Output

This cron job command schedules the execution of the script.sh script located in the /downloads directory every 10 minutes starting from 12:00 AM every day.

List Existing Cron Jobs

For listing the existing cron jobs for a user, type the below command in the terminal:

The output screen like below will be shown listing all the cron jobs for a particular user:

List Existing Cron Jobs

How to Troubleshoot Your Cron Jobs

There are a few things you can try to troubleshoot if a cron job is not working as expected:

  • Check the crontab file to make sure the syntax is correct. You can use the crontab -e command to edit the crontab file and make any necessary corrections.
  • Look through the system logs for any cron job-related error messages. The /var/log/ directory houses the system logs. Check for any cron-related errors, such as permission problems or failed command execution.
  • Verify the script or command's permissions if the cron job is unable to execute it. 
  • To review the cron job's output, use the mail command. Cron automatically sends all output to the user's email. To read the output of the cron job, use the mail command or your email inbox.

Conclusion

  • In this article, we learned everything about Scheduling tasks with cron.
  • We learned about the basic definition of cron.
  • We learn't how to add cron jobs in Linux.
  • We understood the syntax of using Crontab.
  • We looked at some examples of using Cron Jobs.
  • We looked at some techniques of troubleshooting Crons