Categories
Linux

How to Create and Add Cron Jobs in Linux

Scheduling periodic tasks using Cron in Linux

cron is a Linux program that is used to schedule a command or a script to be executed at a later point in time. It can also be used to schedule periodically running commands and scripts. Programs scheduled using cron are commonly referred to as Cron Jobs. Its main use is for System Administration tasks such as regular backups, regular software updates, and other similar maintenance tasks.

Advertisements

Introduction

cron runs as a daemon in Linux, i.e., as a background process. It allows users to schedule jobs directly with the crontab command, which opens a configuration file called Cron File in an editor. Separate Cron Files are created for each user.

Creating a Cron File and Basic Syntax

The crontab command can be executed with -e flag to edit an existing Cron File. If the file doesn’t already exist, it will be created. If the user is calling the command for the first time and if there are multiple file editors installed on the Linux system, the command will ask the user to choose a default editor from a list of editors.

After choosing the editor, a cron file for the user will be created and opened. You can now specify jobs in the file.

Advertisements

The general syntax for specifying a Cron Job is:

<minute> <hour> <day_of_month> <month> <day_of_week> <command(s)>

Basically, the <command(s)> will run at the specified ‘minute’ (0-59), ‘hour'(0-23), ‘day of month'(1-31), month(1-12), day of week, (0-7, For Sunday, either 0 or 7 can be used) in the Cron Job. To simplify, let’s take an example:

1 2 3 4 5 echo "Hello"

This means the command echo "Hello" will run on every fifth day of the week (Friday) and every 3th day of the month, on every 4th month of the year (April), at time 02:01 (2nd hour first minute).

If same command is to be run everyday at 02:01, the syntax will look like:

1 2 * * * echo "Hello"

The * signifies ‘always’ or ‘for all’, eg. for all months, for all days of week, etc.

Advertisements

The command operator (,) can be used to enter a list of values when the task should be repeated. For example:

0 2,3,4 * * * echo "Hello"

This will run the program at 2am, 3am and 4 am, every day.

Similarly, a hyphen (-) operator can be used to specify a range for which the task shall repeat. For example:

0-20 2 * * * echo "Hello"

This will run the program at 02:00, 02:01, 02:02, and so on up to 02:20.

Finally, we have the slash ( / ) operator. This operator is used to specify an interval value according to which the task will be repeated. Eg. */15 in the minutes field means that the task should be repeated every 15 minutes. 2-10/2 in the hours field specifies that the task shall repeat between 2 AM and 10 AM after every 2-hour interval (2 AM, 4 AM, 6 AM, 8 AM, 10 AM).

*/15 2-10/2 * * * echo "Hello"
Advertisements

After you have made the entry in the Cron File, save the file and exit the editor.

You should see a ‘installing new crontab’ message in the terminal after saving and exiting the crontab file.

Macros

Certain macros are predefined in Cron which specify some commonly required time intervals, such as every hour, every day, every month, etc.

To run a task once everyday at start of day, i.e., at 00:00, use macro @daily. This is equivalent to 0 0 * * *.

You can put this in the Cron file in the same way as described before.

Advertisements

In a similar fashion, the other macros can be used, viz. @hourly (Minute 0 of every hour), @monthly (00:00 of the first day of the month), @weekly (00:00 of the first day of week, @yearly(00:00 of first of January every year), @reboot (on every start of the computer).

Conclusion

In this article, we learned how to add Cron Jobs for regular execution in Linux. Proper use of Cron Jobs comes handy for even the most irksome manual tasks faced by the user, Eg. regular deletion of old logs, archiving all kinds of cold data (data which is rarely accessed), etc.