How to Set Up a Cron Job in Linux

January 9, 2020

Introduction

The Cron daemon is a built-in Linux utility that runs processes on your system at a scheduled time. Cron reads the crontab (cron tables) for predefined commands and scripts.

By using a specific syntax, you can configure a cron job to schedule scripts or other commands to run automatically.

This guide shows you how to set up a cron job in Linux, with examples.

tutorial on how to set up linux cron job

Prerequisites

  • A system running Linux
  • Access to a command line/terminal window (CtrlAltT or CtrlAltF2)
  • A user account with root or sudo privileges

Basic Crontab Syntax

Cron reads the configuration files for a list of commands to execute. The daemon uses a specific syntax to interpret the lines in the crontab configuration tables.

To be able to set up a cron job, we need to understand the basic elements that make up this syntax. The standard form for a crontab line is as follows:

a b c d e /directory/command output

So, the parts of a cron command are:

1. The first five fields a b c d e specify the time/date and recurrence of the job.

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

3. The final segment output is optional. It defines how the system notifies the user of the job completion.

1. Cron Job Time Format

The first five fields in the command represent numbers that define when and how often the command runs. A space separates each position, which represents a specific value.

The table below summarizes possible values for the fields and the example syntax:

FieldPossible ValuesSyntaxDescription
[a] – Minute0 – 597 * * * * The cron job is initiated every time the system clock shows 7 in the minute’s position.
[b] – Hour0 – 230 7 * * *The cron job runs any time the system clock shows 7am (7pm would be coded as 19).
[c] – Day0 – 310 0 7 * * The day of the month is 7 which means that the job runs every 7th day of the month.
[d] – Month0 = none and 12 = December0 0 0 7 *The numerical month is 7 which determines that the job runs only in July.
[e] – Day of the Week0 = Sunday and 7 = Sunday0 0 * * 7 7 in the current position means that the job would only run on Sundays.

2. Command to Execute

The next section specifies the command to execute. It represents the exact directory and filename of the script or commands you want cron to complete. For example:

/root/backup.sh

In our example, the command looks at the root directory of the system and runs the backup.sh script. You may specify any script or command you wish.

3. Output (Optional)

By default, cron sends an email to the owner of the crontab file when it runs. This is a convenient way to keep track of tasks. Keep in mind that regular or minor tasks can fill up your inbox quickly.

As this is an optional feature, you can prevent that scenario by disabling the output email. To turn off email output, add the following string, >/dev/null 2>&1, after the timing and command fields.

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

4. Using Operators (Optional)

For efficiency, cron syntax also uses operators. Operators are special characters that perform operations on the provided values in the cron field.

  • An asterisk (*) stands for all values. Use this operator to keep tasks running during all months, or all days of the week.
  • A comma (,) specifies separate individual values.
  • A dash (–) indicates a range of values.
  • A forward-slash (/) is used to divide a value into steps. (*/2 would be every other value, */3 would be every third, */10 would be every tenth, etc.)

Setting Up a Cron Job

To configure a cron job, open the crontab with a preferred text editor and input the syntax for the command you want to run.

How to Edit the crontab File?

To open the crontab configuration file for the current user, enter the following command in your terminal window:

crontab –e

You can add any number of scheduled tasks, one per line.

cronjob configuration file to set up cron job

Once you have finished adding tasks, save the file and exit. The cron daemon will read and execute the instructions provided.

Remember, Cron does not need to be restarted to apply changes.

Edit crontab for a Different User

To edit the crontab for a another user, enter the following command:

crontab –u other_username –e

Note: If you need to run a cron job on reboot, please refer to our Crontab on Boot guide.

Cron Job Examples

When specifying jobs, use the asterisk to specify all values. Putting a value in one of the fields only runs the command on that value. For example:

* 2 0 * 4 /root/backup.sh

Even though it’s set to run at 2 am, it only runs when the first of the month (0) falls on a Wednesday (4). If you change to the following:

* 2 0 * * /root/backup.sh

The command runs the first of every month at 2 am. The following table provides a few basic commands using the /root/backup.sh file from our previous examples.

Cron JobCommand
Run Cron Job Every Minute* * * * * /root/backup.sh
Run Cron Job Every 30 Minutes30 * * * * /root/backup.sh
Run Cron Job Every Hour0 * * * */root/backup.sh
Run Cron Job Every Day at Midnight0 0 * * * /root/backup.sh
Run Cron Job at 2 am Every Day0 2 * * * /root/backup.sh
Run Cron Job Every 1st of the Month0 0 1 * * /root/backup.sh
Run Cron Job Every 15th of the Month0 0 15 * * /root/backup.sh
Run Cron Job on December 1st – Midnight0 0 0 12 * /root/backup.sh
Run Cron Job on Saturday at Midnight0 0 * * 6 /root/backup.sh

Using Special Characters

You can use the slash to divide a time string into steps. To run a backup every 15 minutes:

*/15 * * * *

The * means all values, and the /15 counts and repeats every 15th minute.

Use the dash character to specify a range. To run the code every weekday at 4 am:

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

In this case, 1-5 specifies Monday – Friday.

Use a comma to specify individual instances when the code should run:

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

This would run the code at 4 am on Tuesday and Thursday.

Some wildcards can be combined. Make the command run every other day at 37 minutes past the hour:

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

1-23 specifies the range of hours, /2 sets the interval to every other hour.

List Existing Cron Jobs

You can list all cron jobs on your system without opening the crontab configuration file. Type in the following command in a terminal window:

crontab –l
Terminal output for the crontab list command in Linux

Note: Check out the post on the at command, the alternative for cron job.

Conclusion

You now have a good understanding of how to use cron to schedule tasks in Linux. Use the examples presented in this tutorial to create and schedule cron jobs on your system. Over time, expand the tasks by using special characters to automate most of your mundane tasks.

For more tutorials on Cron jobs, read our Kubernetes CronJob guide.

Was this article helpful?
YesNo
Vladimir Kaplarevic
Vladimir is a resident Tech Writer at phoenixNAP. He has more than 7 years of experience in implementing e-commerce and online payment solutions with various global IT services providers. His articles aim to instill a passion for innovative technologies in others by providing practical advice and using an engaging writing style.
Next you should read
How to Use The APT-GET Command In Linux
May 6, 2019

Advanced Package Tool (APT) is a package management system used on Debian, Ubuntu and other Linux...
Read more
How to Unzip a ZIP File in Ubuntu / Linux
December 2, 2019

Zip files are ubiquitously used on all operating system, including Linux. This simple guide explains how to...
Read more
How to List Users in Linux, List all Users Command
August 4, 2022

Linux OS is unique because of its multiuser characteristic. It allows multiple users on one system, at the...
Read more
How to List, Display, & View all Current Cron Jobs in Linux
August 14, 2019

Cron is used to schedule scripts and commands on Linux systems. Find out where these jobs are stored and list...
Read more