How to add a cron job in Linux?

How to add a cron job in Linux?

We do many chronic jobs every day starting from brushing our teeth, ending with having 7 hours of sleep every day. When it comes to the maintenance of the project, we do similar re-occurring tasks. A cron job in Unix-like systems is a powerful tool for the automation of such time-bound-repeated-tasks. It is a scheduler that executes a given task on the specified time.

They are known for their efficiency and reliability.

Adding a Unix Cron Job involves defining a schedule and the command you want to run at that time. I’ve provided the steps below.

Syntax

Cron jobs are defined in a file called crontab. Each line in this file specifies a schedule and the command to be executed. The schedule uses five fields, separated by spaces:

  • Minute (0-59)
  • Hour (0-23)
  • Day of month (1-31)
  • Month (1-12)
  • Day of week (0-7, where 0 and 7 are Sunday)

You can use special characters for more complex scheduling:

  • *: Matches any value in that field (e.g., * in the minute field runs the job every minute)
  • /: Defines a range of values (e.g., 0/15 in the minute field runs the job every 15 minutes)
  • ,: Separates multiple specific values (e.g., 10,20 in the minute field runs the job at the 10th and 20th minute)

Example

An example of a cron job that should get executed every hour looks like this.

0 * * * * /opt/aggregator/job.sh

Here is how I added a job to Linux.

Listing the cron jobs

The crontab -l command is used to list the currently scheduled cron jobs for your user account.

$ crontab -l
no crontab for aggregator

This says my user (aggregator) does not have any scheduled jobs.

Creating a new job

This table is useful to schedule our job.

* * * * The command to be scheduled
| | | | |
| | | | +------------------ day of week: 0 - 6 (Sunday=0)
| | | +-------------------- month: 1 - 12
| | +---------------------- day: 1 - 31
| +------------------------ hour: 0 - 23
+-------------------------- minute: 0 - 59

* – any value. A * in minute field runs the job every minute. I create a file cronjob1, where I specify the cronjob frequency and job to be executed.

Create a cron job file. I used the name cronjob1. I use vi to create this. You can use your favorite editor.

$ vi cronjob1

Enter the preferred duration and script to be executed by the cron. I want to run it every hour.

0 * * * * /opt/aggregator/start-parser

Add this script to cron

$ crontab cronjob1

Verify if the job is added to cron.

$ crontab -l
0 * * * * /opt/aggregator/start-parser

Yes it is added.

Monitor Cron jobs

How to verify if the cron job is running properly? What are the various things to ensure to monitor cron jobs? These are important questions an SRE engineer to ask himself.

This post is more specific to CentOS. Cron logs are written to /var/log/cron. The following command will fetch you all the lines

# cat /var/log/cron

In a production environment, the log may be bigger. The following log may help you to get last 20 lines.

# tail -20 /var/log/cron

The above job writes the following lines every time it runs.

Feb 18 19:00:01 SINUX05 CROND[19428]: (aggregator) CMD (/opt/aggregator/start-parser)
Feb 18 19:01:01 SINUX05 CROND[20061]: (root) CMD (run-parts /etc/cron.hourly)
Feb 18 19:01:01 SINUX05 run-parts(/etc/cron.hourly)[20061]: starting 0anacron
Feb 18 19:01:01 SINUX05 run-parts(/etc/cron.hourly)[20070]: finished 0anacron

So you know when it runs, when it ends. An error free log denotes the successful invoke of the job. But it is just about invoking. It does not care if the job is successful or not. I talk about the scope of the application, not about the cron job. The job might be invoked correctly, but the script might have failed due to any error in the application, infra or network. So, the job should write meaningful error logs on its own. So a regular review of the application log ensures the successful execution of the job.

This post is written as part of #WriteAPageADay campaign of BlogChatter