Skip to main content

How To Schedule Jobs in Linux | Cron and Crontab Commands


How To Schedule Jobs In Linux
While using a computer system, often one faces the need to carry out certain repetitive jobs on a schedule. Instead of manually executing the requisite commands each and every single time, you can make things easier for yourself by using the Linux Cron utility and
the Crontab command. Wondering how to schedule jobs in Linux using these commands? Let’s find out.
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
You can also check out the complete Linux command reference. Happy learning.
Got any doubts, queries of your own, or any suggestions on the topics that you would like us to cover? Share your views with us.

Comments

WHAT'S HOT

Fappening 2.0 Continues: Modern Family Actress Sarah Hyland Has Become The Latest Victim Of Notorious Hackers [Private Photos, Video Leaked]

Modern Family actress Sarah Hyland has become the latest victim of notorious hackers. Her private pictures and video have been posted online on infamous website Celeb Jihad. According to media sources, Sarah’s lawyers are planning to take legal action against the websites who choose to publish the leaked material. Meanwhile, the actual powers behind the leak are unknown.

Samsung Overtakes Fitbit In Wearable Sales For First Time

Fitbit has been a top competitor on the global wearable market for a long time, but the South Korea giant Samsung managed to steal the silver crown of the wearable market from Fitbit. According to Strategy Analytics, Samsung gained the

Supermassive Black Holes Found Orbiting Each Other For The First Time

Image Credit: UCR Researchers from Stanford University have identified super-massive binary black holes at the center of Galaxy 0402+379 about 750 billion light years away. The two of the black holes are just 24 light years apart and one of them is orbiting the other. This is the first

Owners Of The Samsung Gear S3 In The United Kingdom Have Finally Received A Software Update That Enables Support For Samsung Pay

Owners of the Samsung Gear S3 in the United Kingdom have finally received a software update that enables support for Samsung Pay. Starting today, users will be able to use the Gear S3 smartwatch to make payments on all contactless