A Linux feature known as the Cron daemon allows you to schedule the execution of certain system processes. Cron searches for predefined commands and scripts in the crontab (cron tables).

You can set up a cron job to automatically schedule scripts or other commands to run by following a specific syntax.

With examples, this tutorial demonstrates how to configure a cron job in Linux.

Prerequisites

  • Linux-based system
  • the ability to open a terminal window or command line (Ctrl-Alt-T or Ctrl-Alt-F2)
  • an individual account with root or sudo rights

Crontab syntax basics

Cron checks the configuration files to see what commands should be run. The lines in the crontab configuration tables are interpreted by the daemon using a particular syntax.

We must comprehend the fundamental components of this syntax in order to configure a cron job. A crontab line should be formatted as follows:

a b c d e /directory/command output

The components of a cron command are as follows:

1. The first five fields—a, b, c, d, and e—describe the job’s time/date and frequency.

2. The /directory/command in the second section specifies the location and script you want to run.

3. There is an optional final segment output. It describes the system’s notification procedure for users when a job is finished.

1. Cron Job Time Format

Numbers that specify when and how frequently the command runs are represented by the first five fields in the command. Each position, which denotes a particular value, is separated by a space.

Possible values for the fields are listed in the table below, along with an example of the syntax:

Field Potential Values Syntax Description
[a] – Minute 0 – 59 7 * * * * The cron job starts each time the system clock displays 7 for the minute.
[b] – Hour 0 – 23 0 7 * * * When the system clock reads 7am (7pm would be coded as 19), the cron job starts running.
[c] – Day 0 – 31 0 0 7 * * The job runs every seventh day of the month because it is the seventh day of the month.
[d] – Month 0 = none and 12 = December 0 0 0 7 * The job only runs in July because July is the only month with the numerical value of 7.
[e] – Day of the Week 0 = Sunday and 7 = Sunday 0 0 * * 7 As it stands, position 7 would only be available on Sundays.

2. Instruction to Perform

The command to be executed is described in the following paragraph. It displays the precise path and filename of the script or commands you want Cron to run. For instance:

/root/backup.sh

In our illustration, the command executes the backup.sh script after scanning the system’s root directory. Any script or command can be used as a specification.

3. Results (Optional)

When it executes, cron by default notifies the owner of the crontab file via email. It is practical to manage tasks in this manner. Remember that routine or small tasks can quickly cause your inbox to become full.

You can avoid that situation by turning off the output email because this feature is optional. Add the following code after the timing and command fields to stop email output: >/dev/null 2>&1.

* * * * * directory/command >/dev/null 2>&1

4. Making Use of Operators

Cron syntax also makes use of operators for efficiency. Operators are unique characters that carry out operations on the cron field’s supplied values.

  • Use this operator to keep tasks running on all days of the week or all months by specifying an asterisk (*), which stands for all values.
  • When separating distinct individual values, use a comma (,).
  • A range of values is denoted by a dash (-).
  • A value can be divided into steps by using a forward slash (/), such as */2 for every other value, */3 for every third, */10 for every tenth, etc.

Making a Cron Job

The syntax for the command you want to run must be entered into the crontab when configuring a cron job.

How Can the Crontab File Be Edited?

Enter the following command in your terminal window to view the crontab configuration file for the current user:

crontab –e

Any number of scheduled tasks may be added, one per line.

Save the file after adding all of the tasks, then quit. The cron daemon will read and execute the instructions provided.

Keep in mind that Cron does not require restarting to apply changes.

Crontab for a Different User to Edit

The following command should be entered to change the crontab for a different user:

crontab –u other_username –e

Note: Please refer to our Crontab on Boot guide if you require a cron job to run every time you reboot your computer.

Examples of Cron Jobs

To specify all values when specifying jobs, use the asterisk. When a value is entered in a field, only that value is used to execute the command. For instance:

* 2 0 * 4 /root/backup.sh

It is only activated even though it is set to run at 2 a.m. when the first day of the month ((0)) falls on a Wednesday ((4)). If the following is changed:

* 2 0 * * /root/backup.sh

The command is executed at 2 AM on the first of each month. The basic commands in the following table utilize the /root/backup.sh file from our earlier examples.

Cron Job Command
Every minute, execute a cron job * * * * * /root/backup.sh
30 minute cron job execution 30 * * * * /root/backup.sh
every hour, run the cron job 0 * * * */root/backup.sh
every day at midnight, run the cron job 0 0 * * * /root/backup.sh
every day at 2 am, run the cron job 0 2 * * * /root/backup.sh
Run a Cron job on the first of every month. 0 0 1 * * /root/backup.sh
every 15th of the month, run a Cron job 0 0 15 * * /root/backup.sh
Run a cron job at midnight on December 1st. 0 0 0 12 * /root/backup.sh
On Saturday at midnight, run Cron Job. 0 0 * * 6 /root/backup.sh

Making Use of Special Characters

A time string can be broken up into steps using the slash. Each 15-minute backup cycle:

*/15 * * * *

The /15 counts and repeats every 15 minutes, and the * stands for all values.

To specify a range, use the dash character. Every weekday at 4 AM, the code should be executed:

0 4 * * 1-5 /root/backup.sh

1-5 indicates Monday through Friday in this situation.

For each instance where the code should be executed, separate the names with a comma:

0 4 * * 2,4 /root/backup.sh

This would execute the code every Tuesday and Thursday at 4 a.m.

You can combine some wildcards. Create a schedule for the command to run every other day at 37 minutes past the hour:

37 1-23/2 * * * /root/backup.sh

Hours 1-23 are specified, and /2 makes the interval every other hour.

List of Current Cron Jobs

Without opening the crontab configuration file, you can list every cron job on your system. In a terminal window, type the following command:

crontab –l

![Terminal output for the crontab list command in Linux](https://lh5.googleusercontent.com/mfsqRzkjjyumWDNfQnCVyCyFCesPADVarJrLpNd4aSB1ADwY6XAsaS1AIK76gebbsIuB3YQqR_EGxUdEO5YHL6tNzSUYS4gCkk6qfyxEKHWnJcYZ6_1SEpJNqMzHvW_uiINLWM7iPKEoqRs5xqTt2YqP8vsOvJYRZ9Cr0SccIxFeZY-la5rLIqT0lFMkLQ “List of Current Cron Jobs” =624x412)

Conclusion

You now have a solid understanding of how to schedule tasks in Linux using cron. To create and schedule cron jobs on your system, use the examples provided in this tutorial. The majority of your repetitive tasks can be automated over time by expanding the tasks and using special characters.