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 that you can find and use in any Linux or Unix operating system. Using the cron is a popular way for periodically running 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, and so on.

Cron is named after the Greek word “Chronos” which is used for time. This guide provides an overview of how to schedule jobs with crontab and how to use 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
Field Possible Values Syntax Description
Minute 0 – 59 11 * * * *  The cron job is initiated every time the system clock shows 11 in the minute’s position.
Hour 0 – 23 0 1 * * * The cron job runs any time the system clock shows 1 AM (1 PM would be coded as 13).
Day 0 – 31 0 0 19 * *  The day of the month is 19 which means that the job runs every 19th day of the month.
Month 0 = none and 12 = December 0 0 0 7 * The numerical month is 7 which determines that the job runs only in July.
Weekday 0 = Sunday and 7 = Sunday 0 0 * * 1 1 in the current position means that the job would only run on Mondays.
Command Special The 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”. 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 week) match 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:

String Meaning
@reboot Runs the specified command once, at startup.
@yearly, @annually Both 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.
@monthly Runs the job once a month, on the 1st, at 12:00 AM. In standard cron syntax, this is equivalent to “0 0 1 * *”
@weekly Runs the job once a week at 12:00 AM on Sunday. In standard cron syntax, this is equivalent to “0 0 * * 0”
@daily, @midnight Both run the cronjob every day at 12:00 AM. This is equivalent to specifying “0 0 * * *” in the crontab file.
@hourly Runs 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 by using a specific cron job format for the cron daemon to execute. You can use the crontab command with the following options:

Command Description
crontab -e Edit your crontab file or create one if it doesn’t already exist.
crontab -l Display your crontab file.
crontab -r Remove your crontab file.
crontab -u user Used in conjunction with other options, this option allows you to modify or view the crontab file of the user. Only administrators can use this option.

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

crontab -e

There is no need to restart cron after changing the crontab file. 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

To edit the crontab file that belongs to root or another user you must become root. 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

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

Conclusion

You now have a good understanding of how to use the cron job format to schedule tasks in Linux. Use the examples presented in this guide to create and schedule cron jobs on your system. When combined with shell scripts, you can automate tasks that are normally tedious or complicated.

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

Leave a Reply