Introduction

Cron is a built-in Linux utility used to automate or schedule scripts or other commands at specific or recurring times.

This guide will show you how to set up or create 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

Setting Up a Cron Job

Cron is a daemon that runs every minute on your Linux system. It looks at the crontab (cron tables) for predefined commands and scripts. Like most Linux commands, cron reads the configuration files for a list of commands to execute.


Note:  By default, cron tables open in the vi text editor.


Open the Cron Table for Editing

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

crontab –e

cronjob configuration file to set up cron job

To edit the crontab for a different user, enter the following:

crontab –u other_username –e

Cron Syntax

The cron daemon interprets the lines in the crontab configuration tables as follows:

a b c d e /directory/command output

The first segment a b c d e specifies the timing and recurrence of the job. The second segment /directory/command specifies the location and script you want to run. The final segment output is optional.

To use cron, open your crontab for editing and input the syntax for the command you want to run. You can add any number of scheduled tasks, one per line. Once you have finished adding tasks, save the file and exit.


Note:  Cron does not need to be restarted to apply changes.


Timing

The first 5 characters are numbers that code the timing of the command. Insert a space between each numerical entry. Each position is coded as follows:

a – Minute value (between 0 – 59)

b – Hour value (between 0 – 23)

c – Day value (between 0 – 31)

d – Month value (between 0 = none and 12 = December)

e – Day of the week value (between 0 = Sunday and 7 = Sunday)

Cron will run the task whenever the system value matches the value in each position.

syntax for cron job timing

Using Operators

In addition to numerical codes, cron syntax 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 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.)

See the examples below for more details.

Command to Execute

The next section specifies the command to execute. You’ll need to specify the exact directory and filename of the script or command you want to run. For example:

/root/backup.sh

This would look at the root directory of your system, and run the backup.sh script. You may specify any script or command you wish.

Output

By default, cron sends an email to the owner of the crontab file when it runs. This is a handy way to keep track of tasks. For regular or minor tasks, this can fill up an inbox quickly. Disable the output email by adding the following string at the end:

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

The string >/dev/null 2>&1 turns off email output.

Usage of Cron Jobs

Each time entry field specifies the numerical time when that job should run:

  • Minutes:[7 * * * *] The numerical minute the job runs (7 would be any time the system clock shows 7 in the minute’s position)
  • Hours:[0 7 * * *] The numerical hour the job runs (7 would be any time the system clock shows 7 am while 7 pm would be coded as 19.)
  • Days:[0 0 7 * * ] The numerical day of the month the job runs (7 would be the 7th day of the month.)
  • Months:[0 0 0 7 *] The numerical month the job runs (7 would only run in July.)
  • Day of Week:[0 0 * * 7] The numerical day of the week the job runs (7 would only run on Sunday.)

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

* 2 0 * 4 /root/backup.sh

Even though it’s set to run at 2 am it will only run 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 will run the first of every month at 2 am.

Cron Job Examples

Start by listing any cron jobs your system, with the following command in a terminal window:

crontab –l

To run a backup every minute of every day, use the following:

* * * * * /root/backup.sh

If you want it to run on the 30th minute of every hour of every day:

30 * * * * /root/backup.sh

Run the command every hour of every day:

0 * * * */root/backup.sh

Run a backup every day at midnight:

0 0 * * * /root/backup.sh

To run a backup at 2 am every day:

0 2 * * * /root/backup.sh

To run a backup on the first of every month:

0 0 * * * /root/backup.sh

To run a backup on the 15th of every month:

0 15 * * * /root/backup.sh

Run a backup only on December 1 at midnight:

0 0 0 12 * /root/backup.sh

Run a backup on Saturday at midnight:

0 0 * * 6 /root/backup.sh

Using Special Characters

You can use the slash to divide a time 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.

Conclusion

You should now have a solid understanding of how to use cron to schedule tasks in Linux. Get started using automated commands in Linux with periods, intervals, and dates.

Next you should also read