Wednesday, September 19Care to be aware

How To Schedule Jobs in Linux | Cron and Crontab Commands

Quite like the TaskScheduler utility of Windows, one can utilize the Cron utility to schedule jobs in Linux to run periodically at fixed intervals of time. Used typically for automating system maintenance or administration jobs like backing up data, updating packages and much more, any Linux sysadmin would vouch for its importance.

Job scheduling in Linux: Basic Guide

What is Cron?

The Cron service is a time-based job scheduling service that is typically started when the system boots. It checks every minute for any scheduled jobs and runs them if they exist.

What is Crontab?

In order to manipulate the job schedules, we use the Crontab program in Linux. Crontab, short for ‘cron table,‘ is a configuration file. Each line of the Crontab represents a job and contains information on what to run and when to run. The following is the format for the Linux Crontab:

Schedule Jobs In Linux Crontab Format

The command gets executed whenever all the time specification fields match the current date and time. More often than not, we use the asterisk (*) symbol in the time specification field to match any value in that field.

Opening and Editing Crontab

Now, let us learn how to open and edit the Crontab file, which is an important step in Linux job scheduling. In order to do so, we use the following command:

After executing this command, you might be prompted to choose an editor. If you are a beginner, I would advise you to select Nano, otherwise feel free to go with any editor that you are comfortable with.

Schedule Jobs In Linux Editor Prompt

After you are done selecting the editor, a file similar to the one shown in the screenshot below will open up:

Schedule Jobs In Linux Editor Crontab File

Note: The Hash (#) Symbol is used to denote comments. These comments will be ignored by Cron.

Scheduling Jobs

Now in order to schedule jobs in Linux, all you need to do is enter all the necessary details while following the format mentioned above. Here is an example: Suppose I want to run the command usr/bin/backup at 2:30 AM on the first day of every month, then I will add the following line to the Crontab:

30 : 30th Minute
02 : 2 AM
1   : 1st Day
*   : Every Month
*   : Every Day of the Week

Specifying Multiple Value and Ranges

It is also possible to schedule jobs in Linux to occur at multiple times. Just use a comma (,) to separate the required values. As an example, lets again consider the previous example. Now if i wish to execute the command at 2:30 PM as well, all i need to do is this:

30      : 30th Minute
02,14 : 2 AM and 2 PM
1        : 1st Day
*        : Every Month
*        : Every Day of the Week

Note: We have specified 2 PM by 14 , as Crontab utilizes the 24-hour time format

What’s more, its even possible to specify a range of time within Crontab in Linux job scheduling. Just insert the values separated by a dash (). Here, we will again consider our initial example for showing how it’s done. This time, suppose we wish to execute the command at every hour between 2 AM and 2 PM on the first day of every month. We will do this in the following fashion:

00       : 0th Minute
02-14 : 2AM, 3AM, 4AM, 5AM, 6AM, 7AM, 8AM, 9AM, 10AM, 11AM, 12AM, 1PM, 2PM
1        : 1st Day
*        : Every Month
*        : Every Day of the Week

Crontab Shortcuts

As a bonus in this article on how to schedule jobs in Linux, consider the following shortcuts and keywords of the Crontab format which might come in handy to schedule jobs in Linux:

@yearly       : run once a year at midnight on the morning of January 1
@annually   : same as @yearly
@monthly   : run once a month at midnight on the morning of the first day of the month
@weekly     : run once a week at midnight on the morning of Sunday
@daily         : run everyday at midnight
@midnight  : same as @daily
@hourly      : run once a hour at the beginning of the hour

For example: Consider that I have to run the /usr/bin/backup command every month. The Crontab format for it will be:

Once you save the changes to the Crontab and exit, you shall see the following message indicating that you carried out the process successfully.

Schedule Jobs In Linux Editor Crontab Shortcut

Leave a Reply

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