How to configure Cron Jobs in Linux

Share this article

As a system administrator, you need to run many tasks as your daily activities like taking backup, running network scripts, and many more things but doing the same job daily or regularly performing the same tasks in a few hours is cumbersome and time-consuming. So why not let Linux take care of this for you.

In the article, we will explain how to configure a cron job or how to schedule a task to run at a specific point in time with help of crontab but first let’s understand what is cron job and crontab.

Cron is a Linux utility that runs like a daemon (a background process) executing commands or scripts at a specific time and these commands or scripts are called cron jobs.

To tell the daemon about running these jobs we configure it in crontab. Crontab is a command-line utility that maintains crontab files for individual users. In the next section, we will discuss the commands which crontab provides.

Crontab commands

Usage:

crontab [ -u user ] file
crontab [ -u user ] [ -i ] { -e | -l | -r }

crontab command options:

-u user To tell the crontab as to which user you want to interact (edit, remove or list) with crontab. If you don’t mention any user it will give the output according to your login user.
-i this option is used to show the y/n prompt before deleting user’s crontab
-eused for edit user’s crontab
-l to list users’ crontab
-rto delete user’s crontab

In the following code section, you can see the listing of root crontab in the Linux environment which runs two scripts one script every hour and the other every six-hour.

[root@kolla-controller1 ~]# crontab  -l 
# backup_incremental will run every hour
# backup_incremental will run in every six hour and in a day create 4 full backups. 
0 * * * * /etc/kolla/scripts/backup_incremental.sh
0 */6 * * * /etc/kolla/scripts/backup.sh
[root@kolla-controller1 ~]# 

Configuring crontab

In the previous section, we saw root crontab where we configure it to run two commands (script) where one run every hour and one run every six hours. in this section, we will discuss how we will configure the different parameters to run cronjobs also we will discuss some important examples also.

First of all open the crontab for the user, if you want to configure crontab to the other user mention the user parameter in the command, see the syntax of the command

crontab -u <user_name> -e

Once you open the crontab now it’s time to configure the parameters. There are total of 6 parameters that take care of running your script or command at your chosen time. In the code section, we have numbered columns from 1to 6. As you can see in the code section they are self-descriptive. From 1-5, you mention the time and the 6th column is for which command you want to run.

1    2    3    4    5                6
*    *    *    *    *   <Command or script which we want to run>
|    |    |    |    |       
|    |    |    |    Day (0-6) ( start from Sunday = 0 )
|    |    |    |
|    |    |    Month (1-12)
|    |    |
|    |    Month's date (1-31)
|    |
|    Hour (0-23)
|
Minute (0-59)

Before discussing the examples following table shows multiple operators which are used to set the timings of a cron job.

*any value
,value list separator
range of values
/step values

Crontab examples

Running job every hour

Below mention command will run every hour at hr:00.

0 * * * * /usr/bin/test.sh

Running job at 9:10 AM

10 9 * * * /usr/bin/test.sh

Running job every 4 hours

Below entry will run /usr/bin/test.sh every 4 hours at hr:00.

0 */4 * * * /usr/bin/test.sh

Running job from 9:00-17:00 from Monday to Friday every hour

0 9-17 * * 1-5 /usr/bin/test.sh

Running job on January 12th on 9:30

30 9 12 1 * /usr/bin/test.sh

Running job at 7,8 and 11:00

0 7,8,11 * * * /usr/bin/test.sh

Running multiple job with same cron entry (at 7,8 and 11:00)

0 7,8,11 * * * /usr/bin/test1.sh; /usr/bin/test2.sh

Summary

In this article, we discussed a very important tool (crontab) of a sys admin. Which they use to automate the running of the scripts or commands at a specific time. This tool comes handy when you want to run tasks like taking backup, running scripts, etc. daily or after some interval or at a specific time.

1 thought on “How to configure Cron Jobs in Linux”

Leave a Comment

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