How to Set Up a Cron Job in Linux

Introduction

Cron jobs are an essential part of Linux systems. They allow you to schedule tasks to run at specific times and intervals. Setting up a cron job in Linux is a relatively simple process, but it can be intimidating for those who are unfamiliar with the command line. This guide will walk you through the steps of setting up a cron job in Linux, from creating the job to scheduling it to run. By the end of this guide, you will have a better understanding of how to set up a cron job in Linux.

How to Set Up a Cron Job in Linux

1. Open a terminal window.

2. Type crontab -e to open the crontab editor.

3. Enter the cron job command.

4. Save the crontab file.

5. Exit the crontab editor.

6. Verify that the cron job is running by typing crontab -l.

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:

Field Possible Values Syntax Description
[a] – Minute 0 – 59 7 * * * *  The cron job is initiated every time the system clock shows 7 in the minute’s position.
[b] – Hour 0 – 23 0 7 * * * The cron job runs any time the system clock shows 7am (7pm would be coded as 19).
[c] – Day 0 – 31 0 0 7 * *  The day of the month is 7 which means that the job runs every 7th day of the month.
[d] – Month 0 = none and 12 = December 0 0 0 7 * The numerical month is 7 which determines that the job runs only in July.
[e] – Day of the Week 0 = Sunday and 7 = Sunday 0 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 Job Command
Run Cron Job Every Minute * * * * * /root/backup.sh
Run Cron Job Every 30 Minutes 30 * * * * /root/backup.sh
Run Cron Job Every Hour 0 * * * */root/backup.sh
Run Cron Job Every Day at Midnight 0 0 * * * /root/backup.sh
Run Cron Job at 2 am Every Day 0 2 * * * /root/backup.sh
Run Cron Job Every 1st of the Month 0 0 1 * * /root/backup.sh
Run Cron Job Every 15th of the Month 0 0 15 * * /root/backup.sh
Run Cron Job on December 1st – Midnight 0 0 0 12 * /root/backup.sh
Run Cron Job on Saturday at Midnight 0 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.

How to Set Up a Cron Job in Linux

Cron jobs are an essential part of Linux systems. They allow you to schedule tasks to be run at specific times, such as running a backup script every night or sending out an email report every week. In this tutorial, we’ll show you how to set up a cron job in Linux.

Step 1: Access the Cron Job Interface

The first step is to access the cron job interface. This is usually done by running the crontab command. This will open up the cron job editor in your default text editor. If you’re not sure which text editor is your default, you can run the command “echo $EDITOR” to find out.

Step 2: Add a Cron Job

Once you’re in the cron job editor, you can add a new cron job by typing in the command you want to run, followed by the schedule you want it to run on. The schedule is specified using a special syntax, which looks like this:

  * * * * * command to be executed
  - - - - -
  | | | | |
  | | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
  | | | ------- Month (1 - 12)
  | | --------- Day of month (1 - 31)
  | ----------- Hour (0 - 23)
  ------------- Minute (0 - 59)

For example, if you wanted to run a backup script every night at midnight, you would use the following syntax:

  0 0 * * * /path/to/backup/script.sh

Step 3: Save the Cron Job

Once you’ve added the cron job, you need to save it. To do this, press Ctrl+X to exit the editor, then type “Y” to save the changes. The cron job will now be active and will run according to the schedule you specified.

Step 4: Test the Cron Job

It’s always a good idea to test the cron job before relying on it. To do this, you can run the command manually to make sure it works as expected. If everything looks good, you can be confident that the cron job will run correctly.

Conclusion

In this tutorial, we’ve shown you how to set up a cron job in Linux. We’ve covered how to access the cron job interface, add a cron job, save the cron job, and test the cron job. With these steps, you should be able to set up cron jobs with ease.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?