fbpx

How To Configure a Cron Job

Do you spend hours on tedious and repetitive server administration tasks? If you answered yes, you’d enjoy this article, which explains how cron, cPanel, and WHM can help you automate these tedious but necessary aspects of web hosting.

Cron is a time-based scheduling tool. It enables server administrators to run scripts known as cron jobs at predefined intervals. cPanel and WHM use this method to run several necessary server maintenance scripts, and you or your users can also schedule scripts. You could, for example, schedule a custom backup script to run every morning at 7 a.m.

We’ll look at common cron uses, such as scheduling scripts and adding new jobs to automate server administration tasks.

Uses of a Cron Job

Cron can execute simple commands, shell scripts, and programs written in PHP, Python, and Perl. You can use the task scheduler to automate tasks that can be completed on the command line or in a script.

Cron job applications include:

  • Log rotation entails moving old logs to an archive at predetermined intervals to prevent them from becoming unmanageable.
  • Emailing users — Notifying users when their resources are running low.
  • Schedule checks for security updates and have them applied automatically.
  • Backups of your content management system and databases ensure that you never forget to perform essential backups.

Configuring a Cron Job in cPanel

Cron represents times and days using a concise notation. A typical configuration line could look something like this:

  • * myscript.sh 30 23 25

The format consists of five white-space separated time interval fields followed by a command.

The schedule is saved in a “crontab” file, which is a list of jobs with extra directives. You can use the crontab command to edit the file over SSH, but using cPanel & WHM is faster and easier, as it includes two interfaces for scheduling jobs: one for cPanel’s maintenance scripts and one for user scripts, which we’ll look at in the next section.

Before we get to the cron configuration interface in cPanel, you should be familiar with interval notation.

From left to right, the time fields represent the following:

Minutes — a number ranging from 0 to 59.
Hours — a number ranging from 0 to 23. Cron operates on a 24-hour basis, so 4 p.m. is 16, 11 p.m. is 23, and so on.
Month’s days — a number between 1 and 31.
Months — a number ranging from 1 to 12.
Weekdays — a number between 0 and 7, with Sunday denoted by either 0 or 7.

Asterisks indicate that the job will run at every relevant interval that is compatible with the other fields.

The time interval “30 23 25 * *” was specified in our first example. This is the thirty-first minute of the twenty-third hour of the twenty-fifth day of the month. To put it another way, 11:30 p.m. on the 25th of each month. Because the Month and Day of Week fields contain asterisks, the script runs only on days and months when the other fields are valid, which is every day and month in this case.

Let’s take a look at some more complicated examples. You want to run a monitoring script every hour, on the hour, between 9 a.m. and 5 p.m., but only on weekdays. To accomplish this, we must introduce a new piece of notation, the range.

  • * 00 9-17 * * 1-5 myscript.sh

A hyphen separates values in a range. The following is an example:

  • 00 — the first (or, more accurately, the zeroeth) minute of the hour. 
  • 9-17 — each hour between 9 a.m and 5 p.m.
  • * — every day.
  • * — every month.
  • 1-5 — the first to the fifth day, so Monday to Friday. 

What if, instead of running the script every hour between 9 a.m. and 5 p.m., you wanted it to run at 9 a.m., 12 p.m., 3 p.m., and 6 p.m. on weekdays? This type of interval requires one more piece of notation, the list.

00 9, 12, 15, 18, * * 1-5 myscript

This example is similar to the previous one, but instead of a range, it uses a list in the Hours field.

The fundamentals we’ve covered should cover almost any situation.

Configuring cPanel Cron Jobs in WHM

In cPanel, select Cron Jobs in the Advanced section of the Main Page menu.

There is a table where you can add new scripts and set their time intervals.

To make configuration easier, we’ve included a drop-down menu with common options like Once Per Week and Once Per Month.

If you don’t see a setting that works for you in the main drop-down menu, enter a time interval in the boxes on the left or use the menus on the right to choose from standard entries for each period.

Finally, in the Command field, enter the command or the path to your script and click the “Add New Cron Job” button to save. Cron will execute the script at the intervals you specify.

Cron is a simple but powerful tool for automating server administration tasks that are mundane, repetitive, and easily forgotten. The interval format can be confusing at first, but a few minutes of study will save you many hours of work in the future, especially if you combine your knowledge with the quick and easy configuration tools in cPanel and WHM.

Michael
WRITTEN BY

Michael

My name is Michael. I'm a passionate Web Designer & Blogger.

Leave a Reply