What Is Crontab? How to Create Cron Jobs on Linux?

Cron is a helpful tool in Linux that allows you to automate tasks using scripts at specific times or intervals. It's commonly used by developers for backup, cleaning, and notifications. Cron jobs quietly run in the background, checking specific files and directories like /etc/crontab, /etc/cron.*/, and /var/spool/cron/. Each user has their unique crontab.

What is Crontab?

Crontab is a list of commands you want to run regularly, and it's also the name of the command used to manage that list. It works with the cron scheduler to execute tasks based on a set schedule. Cron automatically performs tasks for you at specified times, following the schedule you define in the crontab.

Crontab Syntax

To use Cron, we need to know its specific language for reading and executing commands. Setting up a cron job requires understanding the basic elements of this language. The standard format for a cron job is:

a b c d e /directory/command output

So, the parts of a cron command are:

The first five fields (a b c d e) determine when and how often the job runs.

In the second part, (/directory/command) you specify the location and script you want to run.

The last part (output) is optional and decides how the system informs the user when the job is done.

1. Cron Job Time Format

The first five fields in the command show when and how often the command runs. Each position has a specific value, and they are separated by spaces.

Here's a summary of the possible values and their syntax:

[a] – Minute: 0 to 59 | Example: 7 * * * * | This cron job starts whenever the system clock shows 7 in the minute's position.

[b] – Hour: 0 to 23 | Example: 0 7 * * * | The cron job runs whenever the system clock shows 7 am (7 pm would be coded as 19).

[c] – Day: 0 to 31 | Example: 0 0 7 * * | The job runs on the 7th day of the month.

[d] – Month: 0 = none and 12 = December | Example: 0 0 0 7 * | The job runs only in July (the numerical month is 7).

[e] – Day of the Week: 0 = Sunday and 7 = Sunday | Example: 0 0 * * 7 | The job runs only on Sundays.

2. Command to Execute

The next part specifies the command to run. It tells cron the exact directory and filename of the script or commands you want to use.

For example:

/root/backup.sh

In this example, the command runs the backup.sh script from the root directory of the system. You can specify any script or command you want.

3. Output (Optional)

By default, cron sends an email to the crontab file owner after running a job. This helps keep track of tasks, but it can fill up your inbox with regular or minor tasks.

You can prevent this by disabling the output email. To do that, add the following string, >/dev/null 2>&1, after the timing and command fields.

Example:

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

4. Using Operators (Optional)

Cron syntax also uses operators for efficiency. Operators are special characters that perform operations on the values in the cron field.

An asterisk (*) represents all values. It keeps tasks running during all months or all days of the week.

A comma (,) separates individual values.

A dash (-) indicates a range of values.

A forward-slash (/) divides a value into steps. For example, */2 would be every other value, */3 would be every third, */10 would be every tenth, and so on.

How to Set Up a Cron Job?

In this part, we'll learn how to schedule a simple script with a cron job.

Step 1: Create a script named "date-script.sh" that prints the system date and time and adds it to a file. Here's the script:

# nano date-script.sh

Script content:

#!/bin/bash

echo date >> date-out.txt

Step 2: Make the script executable by giving it execution rights:

# chmod +x date-script.sh

Step 3: Add the script to the crontab using the command crontab -e.

Step 4: After entering the command "crontab -e" in the terminal, you'll be prompted to choose an editor. Select the desired editor by entering the corresponding number in the Choose field. This will open the crontab file using the selected editor. In the crontab file, set the script to run every minute:

* * * * * /path/to/date-script.sh

Step 5: Check the "date-out.txt" file's output. The system date should be printed to this file every minute, as specified in the script.

# cat date-out.txt

Manage Cron Jobs

To view all Cron jobs, use this command:

# crontab -l

To view the Cron jobs of a different user, use this command:

# crontab -u username -l

To delete all Cron jobs, use this command:

# crontab -r

To delete a specific user's Cron job, use this command:

# crontab -r -u username

To edit a specific user's Cron jobs, use this command:

# crontab -u username -e

Conclusion

This guide has shown you how to set up Cron jobs on Linux. Cron is a simple yet powerful utility that can help you automate many tasks related to system administration, making your workload lighter.


Was this answer helpful?

« Back

chat