How to Use the Cron Job Format to Schedule Task in Linux

How to Use the Cron Job Format to Schedule Task in Linux

Cron is a scheduling daemon that uses a specific format to execute jobs. Here’s how to use it on Linux for automatic task execution.

Cron is one of the most useful utilities you can find and use in any Linux or Unix operating system. Using the cron is a popular way to run tasks such as commands or shell scripts. These periodic tasks are also known as Cron Jobs and are scheduled to run at a specific time.

Related: 20 Basic Linux Commands for Beginners Explained with Examples

For example, you could set a cron job to automate repetitive tasks such as backing up a database or data, updating the system with the latest security patches, sending emails, etc.

Cron is named after the Greek word “Chronos,” which is used for time. This guide provides an overview of scheduling jobs with crontab and using cron’s special syntax format to run them.

What is the crontab File?

Above all, the crontab file is a simple text file containing a list of commands meant to be run at specified times. It is edited using the crontab command. The commands in the crontab file (and their run times) are checked by the cron daemon, which executes them in the system background.

Each line of a crontab file is either “active” or “inactive.” An “active” line is an environment setting or a cron command entry. An “inactive” line is anything ignored, including comments.

Blank lines and leading spaces and tabs are ignored. Lines whose first non-space character is a pound sign (#) are interpreted as comments and are ignored.

Please be aware that comments are not allowed on the same line as cron commands because they will be interpreted as part of the command. For the same reason, comments are not allowed on the same line as environment variable settings.

Cron Job Format Entries

Each entry in a crontab file consists of six fields, specifying in the following order:

minute hour day month weekday command
Cron Job Format
FieldPossible ValuesSyntaxDescription
Minute0 – 5911 * * * * The cron job is initiated every time the system clock shows 11 in the minute’s position.
Hour0 – 230 1 * * *The cron job runs any time the system clock shows 1 AM (1 PM would be coded as 13).
Day0 – 310 0 19 * * The day of the month is 19 which means that the job runs every 19th day of the month.
Month0 = none and 12 = December0 0 0 7 *The numerical month is 7 which determines that the job runs only in July.
Weekday0 = Sunday and 7 = Sunday0 0 * * 1 1 in the current position means that the job would only run on Mondays.
CommandSpecialThe complete sequence of commands to execute. Commands, executables (such as scripts), or combinations are acceptable.

Any of these fields can be set to an asterisk (*), which stands for “first through last.” So, for example, to run a job every hour, put * in the hour field.

Ranges of numbers are also allowed. Ranges are two numbers separated with a hyphen (-). The specified range is inclusive. For example, 8-11 for an “hours” entry specifies execution at hours 8, 9, 10, and 11.

Lists are allowed. A list is a set of numbers (or ranges) separated by commas (,). Here’s some examples: 1,2,5,9 or 0-4,8-12.

Steps are also permitted after an asterisk, so if you want to say “every two hours,” you can use */2.

The cron daemon checks the crontab once every minute. Commands are executed by cron when the minute, hour, and month fields match the current time, and at least one of the two-day fields (day of month or day of the week) matches the current day.

Cron’s Special Syntax Format

Cron jobs also offer some specially formatted strings, which can be used in place of the five time-and-date fields. They are essentially shortcuts for the equivalent numeric schedule specified:

StringMeaning
@rebootRuns the specified command once, at startup.
@yearly, @annuallyBoth run the specified task every year at 12:00 AM on the 1st of January. This is equivalent to specifying “0 0 1 1 *” in the crontab file.
@monthlyRuns the job once a month, on the 1st, at 12:00 AM. In standard cron syntax, this is equivalent to “0 0 1 * *”
@weeklyRuns the job once a week at 12:00 AM on Sunday. In standard cron syntax, this is equivalent to “0 0 * * 0”
@daily, @midnightBoth run the cronjob every day at 12:00 AM. This is equivalent to specifying “0 0 * * *” in the crontab file.
@hourlyRuns the job at the top of every hour. In standard cron syntax, this is equivalent to “0 * * * *”

How to Use the crontab Command

The crontab command creates a crontab file containing commands and instructions using a specific cron job format for the cron daemon to execute. For example, you can use the crontab command with the following options:

CommandDescription
crontab -eEdit your crontab file or create one if it doesn’t already exist.
crontab -lDisplay your crontab file.
crontab -rRemove your crontab file.
crontab -u userUsed in conjunction with other options, this option allows you to modify or view the user’s crontab file. Only administrators can use this option.

For example, to edit or create your crontab file, type the following command at the shell prompt:

crontab -e

There is no need to restart the cron service after changing the crontab file. Instead, the cron will examine the modification time on all crontabs and reload those which have changed.

How to Edit a crontab File That Belongs to Another User

You must become root to edit the crontab file that belongs to root or another user. But, of course, you do not need to become root to edit your crontab file.

crontab -u [username] -e

How to Remove a crontab File

Most of the time, you won’t want to remove the crontab file. However, you might want to remove some rows from the crontab file.

To remove your user’s crontab file, run the following command:

crontab -r

Cron Job Examples

CommandCron Job
* * * * * /root/script.shRun Cron Job every minute.
0 * * * * /root/script.shRun Cron Job every hour.
0 0 * * * /root/script.shRun Cron Job every day at midnight.
0 2 * * * /root/script.shRun Cron Job at 2 AM every day.
0 0 15 * * /root/script.shRun Cron Job every 15th of the month at midnight.
0 0 0 12 * /root/script.shRun Cron Job on December 1st at midnight.
0 0 * * 6 /root/script.shRun Cron Job on Saturday at midnight.
0 15 * * 1-5 /root/script.shRun Cron Job at 3 PM on every day from Monday through Friday.
*/5 * * * * /root/script.shRun Cron Job every 5 minutes.
0 8-16 * * * /root/script.shRun Cron Job every day, every hour, on the hour, from 8 AM through 4 PM.
0 4 * * 2,4 /root/script.shRun Cron Job at 4 AM on Tuesday and Thursday.
@reboot /root/script.shRun Cron Job when the system starts.

Conclusion

You now understand how to use the cron job format to schedule tasks in Linux. Use the examples in this guide to create and schedule cron jobs on your system.

When combined with shell scripts, you can automate normally tedious or complicated tasks.

For more information, the cron and crontab man pages here and here have excellent information and descriptions of how the cron system works.

Bobby Borisov
Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Leave a Reply

Your email address will not be published. Required fields are marked *