Table of contents
1.
Introduction
2.
Crontab in Linux
3.
Creating a New Crontab
4.
Commands in Crontab
5.
Syntax of Cron Job in Cron Table
6.
Cron Job Field Format
7.
Cron Job Expression Examples
8.
Uses of Special Strings
8.1.
@reboot
8.2.
@yearly
8.3.
@monthly
8.4.
@weekly
8.5.
@daily
8.6.
@hourly
9.
Special Characters in Cron Table
10.
Best Practice in Linux Cron
10.1.
Generating Log Files
10.2.
Handling Time Zones in Cron Jobs
10.3.
Redirecting Output
11.
Importance of Cron Job in Linux
12.
Frequently Asked Question
12.1.
What is a cron job?
12.2.
What fields are there in the cron schedule?
12.3.
How can a user momentarily disable a particular cron job?
12.4.
What are the cron default time variables?
13.
Conclusion
Last Updated: Mar 27, 2024
Easy

Linux Cron Cheat Sheet

Author Ayush Mishra
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Cron is a special feature of the Linux Operating System for automating scheduled tasks. Users can plan actions or scripts to run at predetermined intervals, such as daily backups, log rotation, system maintenance, database optimization, and much more, with the help of a cron job.

Linux Cron Cheat Sheet

In this blog, we will discuss the “Linux Cron Cheat Sheet” for quickly and easily automating repetitive processes in your Linux environment, saving time, and boosting productivity.

Crontab in Linux

Crontab manages and schedules cron jobs by configuration files in Linux and other Unix-like operating systems. They are also known as Cron Table. It enables users to specify commands or scripts to be automatically run at predefined times. Each user on the system can have their Crontab, which contains the jobs they want to perform at specific times.  Crontab functions as a time-based job scheduler.

Creating a New Crontab

In this section of "Linux Cron Cheat Sheet," we will see how to create a new Crontab, following the given below steps:

Step 1: Start a shell session or a terminal

Step 2: To open or create a new Cron table for the current user, type the following command:

crontab -e


Step 3: Select an editor to edit the Cron table if required

Commands in Crontab

In this "Linux Cron Cheat Sheet" section, we will see some commands in Crontab.

  • crontab -e: If a crontab file is not already there, edit it or create one
     
  • crontab -l: It is used to view the crontab file
     
  • crontab -r: It is used to delete the crontab file
     
  • crontab -v: It is used to show the last time your crontab file was modified

Syntax of Cron Job in Cron Table

There are five fields in the syntax of a cron job entry that let users specify when and how often a command or script should be run. The fields represent the following, with spaces separating each field.

Cron Job Syntax

Each field with an asterisk (*) denotes one that defines the Cron Job's timing. The fields, which are separated by spaces, specify the time when the command will be executed.

Cron Job Field Format

The fields in Cron Job Format are:-

  • Minute (0–59): It indicates the minute the cron job is supposed to run. If you set it to 12, the job will start running at 12 minutes of the hour
     
  • Hour (0–23): It indicates when the cron job should be executed. If you set it to 2, for example, the work will start at 2 AM
     
  • Month day (1–31): It indicates the day the cron job should be executed. If you set it to 8, for example, the job will run on the eighth day of each month
     
  • Month (1–12): It indicates the month the cron job shall be executed. By setting it to 6, for example, the task will be executed in June
     
  • Weekday (0–7): It denotes the day of the week the cron job shall execute. Sunday is represented by 0 and 7, Monday by 1, and so on. For example, if you set it to 4, the job will run on Thursdays

Cron Job Expression Examples

In this section of "Linux Cron Cheat Sheet," we will see various examples to understand the working of Cron Job.

Example 1: To run a script daily at 6 AM, run the following command.

0 6 * * * Command_to_executed


Example 2: To run a script daily at 4:40 PM, run the following command.

40 16 * * * Command_to_executed


Example 3: To run a script every Tuesday at 6:00 AM, run the following command.

0 6 * * 2 Command_to_executed


Example 4: To run a script every 20 minutes, run the following command.

* /20 * * * * Command_to_executed


Example 5: To run a script every hour, run the following command.

0 * * * * Command_to_executed


Example 6: To run a script every 4 hours, run the following command.

0 */4 * * * Command_to_executed


Example 7: To run a script every 1st of the month, run the following command.

0 0 1 * * Command_to_executed

Uses of Special Strings

Special strings offer an efficient way to establish the schedule without declaring each field individually. Some special strings are:-

@reboot

This unique string causes the supplied command or script to run once the system boots up.  It is beneficial for tasks that must be carried out as the system boots up.

Example

@reboot /home/user/file.sh

 

@yearly

This unique string stands for "0 0 1 1 *", indicating that the cron job will execute just once annually, on January 1.

Example

@yearly /home/user/file.sh

 

@monthly

This unique string stands for "0 0 1 * *", indicating that the cron job will execute once every month, on the first of the month.

Example

@monthly /home/user/file.sh

 

@weekly

The cron job will execute once a week, on Sunday, according to the special string "0 0 * * 0," which it symbolizes.

Example

@weekly /home/user/file.sh

 

@daily

This unique string stands for "0 0 * * *," indicating that the cron job will be executed once a day at midnight.

Example

@daily /home/user/file.sh

 

@hourly

This unique string stands for "0 * * * *," indicating that the cron job will be executed once every hour at the start of the hour.

Example

@hourly /home/user/file.sh

Special Characters in Cron Table

Special Characters are used to express different scheduling choices and specify timing for running cron jobs. Some special characters used in Cron Table are:-

  • Asterisk: The asterisk is a wildcard that accepts any value. This term denotes "every possible value" for a field in a cron.  The symbol * * * * *, for example, denotes "every minute, every hour, every day, every month, and every day of the week"
     
  • Comma: It denotes a list of values in a cron field. You can use it to run the cron job several times at predetermined times.  For Example, “5,15,25 * * *”  means "at the 5th, 15th, and 25th minute of every hour"
     
  • Hyphen: It is used to designate a range of values for a cron field. It enables you to run the cron job across the entire range that has been defined.  As an illustration, 1-5 * * * * stands for "every minute from 1 to 5 past the hour"
     
  • Slash: In a cron field, the slash is used to specify step values. It enables you to run the cron job regularly. As an illustration, the notation */10 * * * * denotes "every 10 minutes"
     
  • Hash: In a cron table, comments are added using the hash character
     
  • Question Marks: Day of Month and Day of Week can be used instead of the symbol "*  ."In a cron expression, only Day of Week or Day of Month may be used

Best Practice in Linux Cron

In this section of "Linux Cron Cheat Sheet," we will see some best practices while working with Cron.

Generating Log Files

The closing bracket (>) must be used once more if the cron output is to be saved to a file:

* * * * * command_to_be_executed > /path/to/output_file

Handling Time Zones in Cron Jobs

Cron Jobs are executed based on the system's selected time zone. However, you can specify the required time zone within the Cron Job entry if you need to perform a Cron Job based on a different time zone. For Example:

TZ=America/New_York * * * * * command_to_be_executed

Redirecting Output

It directs your cron tasks' output and error messages to log files. Make the following changes to the Cron Job entry to reroute the output:

* * * * * command_to_be_executed > /path/to/output_file 2>&1

Importance of Cron Job in Linux

The importance of Cron Job in Linux is:-

  • System Maintenance: Cron Jobs can carry out standard system maintenance procedures such as disc cleanup activities, updating software packages, and deleting temporary files.
     
  • Website Maintenance: Cron Jobs can run scripts or instructions to carry out operations connected to websites, such as database optimization, content updates, and scheduled publication or republication of articles.
     
  • Scheduled tasks for applications: Many services and applications use cron jobs to do certain operations at predetermined intervals. Cron jobs, for instance, may be used by email servers to send out recurring reports or content management systems to publish scheduled posts.
     
  • Data backups: Automating data backup processes with Cron Jobs ensures that important files and databases are regularly and consistently backed up.

Frequently Asked Question

What is a cron job?

The Cron job is a Linux task scheduled to be executed at predetermined intervals. It is controlled by the cron daemon, which runs scripts or instructions by the timetable specified in the cron table.

What fields are there in the cron schedule?

The minute, hour, day of the month, month, and day of the week are the five fields that make up a cron schedule. The following is the syntax: minute hour day month weekday.

How can a user momentarily disable a particular cron job?

Users can temporarily disable a particular cron task by commenting out the relevant line in the user's cron table. To deactivate a line, start it with a # by editing the cron table using the Crontab -e command. 

What are the cron default time variables?

Cron offers several helpful predefined time macros, @reboot executes the command when the system boots, @daily execute it once per day, and @weekly executes it once per week.  Additionally, macros are available for annual, monthly, and hourly chores.

Conclusion

Crontab, an important tool for job automation on Linux systems, makes it simple for users to plan out repetitive activities, system maintenance, and application-specific processes.  Therefore, this cheat sheet will help you to do hands-on Linux Cron.

We hope this blog has helped you enhance your knowledge of Linux Cron.  Do not stop learning!  We recommend you read some of our articles related to the Linux Cron Cheat Sheet: 


Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more!  If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!

But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc.  For placement preparations, you must look at the problemsinterview experiences, and interview bundles.

We wish you Good Luck! 

Happy Learning!

 

Live masterclass