How to Set Up a Cron Job in cPanel Hosting (+ WP Cron)

How to Set Up a Cron Job in cPanel Hosting (+ WP Cron)

Cron Jobs allow you to run scripts and automate repetitive tasks that can take up considerable time if done manually. In this post, we show you how to set up a cron job in cPanel and how to configure a real cron for WordPress.

This tutorial covers the following:

What is a Cron Job?

A cron job allows you to automate commands or scripts on your site that require being triggered at regular intervals or whenever specific events happen to complete repetitive tasks.

cPanel includes a software utility called cron (from the Greek word for time, Chronos). This utility lets you schedule tasks that your system runs repeatedly. These tasks are called cron jobs or cron schedules and they are run from a file called crontab (CRON TABle) that lists all the tasks that cron needs to execute.

You can set a command or script using cron jobs to run at a specific time every day, week, etc. For example, you could set a cron job to run a backup or delete temporary files to free up disk space on your server every week (or every two hours, or on a specific day of the month, or at 2:36 pm every Wednesday).

Once a cron job is set up, it then runs automatically as per your schedule.

Parts of a Cron Command

A Cron Command typically consists of four parts:

  • Timing: this part lets you set when you’d like your script to run by specifying regular intervals in terms of minutes, hours, days, months, and weekdays.
  • Execute: this part calls the PHP your cron job needs run, which is normally located in the path /usr/bin/php.
  • Script Path: this is the full path of the file you want to run (e.g. /home/user/public_html/index.php)
  • Output: this optional part lets you write the cron output to a file or discard it. For example, to discard the output may require adding something like this: /dev/null 2>&1.

Normally, any script that requires a cron job will provide you with the path or command you need to use that script. Your hosting provider may also supply or give you specific instructions for structuring your cron job commands. You then create a cron job using the path or command provided (and replace any placeholder strings in the line or command with your site’s details, if required).

In the screenshot below, for example, my cPanel dashboard has automatically provided some PHP command examples with my site’s username (helpservices) and a sample PHP version included in the command path.

PHP command examples.

Note: cPanel recommends having good knowledge of Linux commands to use cron jobs effectively and running your script past your hosting administrator before adding a cron job to avoid running into problems.

How to Set Up A Cron Job in cPanel

To set up a cron job in cPanel, log into your cPanel dashboard, then scroll down to the Advanced section and click on Cron Jobs.

cPanel Dashboard - Cron JobsSelect Cron Jobs in cPanel’s Advanced section.

This brings up the Cron Jobs screen. Here, you can set up email notifications (see next section), add new cron jobs, and view a list of all the cron jobs you have set up and are currently running.

Scroll down to the Add New Cron Job section. You can either select select a preset interval for your cron job to run from the Common Settings drop-down menus or enter your own custom values using the following options:

  • Minute: The number of minutes between each time the cron job runs, or the minute of each hour on which you wish to run the cron job.
  • Hour: The number of hours between each time the cron job runs, or the hour of each day on which you wish to run the cron job.
  • Day: The number of days between each time the cron job runs, or the day of the month on which you wish to run the cron job.
  • Month: The number of months between each time the cron job runs, or the month of the year in which you wish to run the cron job.
  • Weekday: The days of the week on which you wish to run the cron job.
Cron - Common SettingsCron – Common Settings text boxes.

The Common Settings menu contains typical cron job configuration settings with numbers or asterisks in parentheses indicating when the cron job should run.

Cron - Common Settings menu.Cron – Common Settings menu.

If you select one of these settings, the system automatically populates the text boxes below with your selected settings.

So, for example, if we select the option to set up a cron job to run a script once per week…

Cron - Common Settings: Weekly schedule.Let’s set our cron job to run once a week.

The text boxes will be automatically populated with the values of our selected setting.

Cron Job text boxes auto-populated with values to run once a week.Cron Job text boxes auto-populated with values to run once a week.

In the example above, our cron job is scheduled to run every Sunday of every month at exactly midnight.

However, you can use the sub-drop-down menus to enter custom values into the text boxes. This lets you easily modify and adjust your cron job’s scheduled intervals.

Cron Job schedule with adjusted settings.Adjust the settings of your cron job using the sub-drop-down menus and text boxes.

The last step is to enter the actual command you want your cron job to run. You must specify the absolute path to the command you wish to run.

Type or paste in the command into the Command: text box and click Add New Cron Job.

Cron job commandType or paste in the cron job command.

This will add your cron job to the Current Cron Jobs list.

Current cron jobsYour cron job has been added to the Current Cron Jobs list.

How to Get Email Notifications After Running Cron Jobs

You can opt to receive (or stop receiving) emails every time a cron job runs a command that produces an output.

This is useful if you want to know when one of your jobs fails to run and you want to review the errors.

Just scroll down to the Cron Email section, enter an email address, and click the Update Email button.

Cron EmailGet notified via email after a cron job is run.

You can also elect not to receive emails after cron jobs are run:

  • To disable email notifications for all cron jobs, remove the email address. To do this, leave the Email field blank and click the Update Email button. The current email address should display as (none).
  • To disable email notifications for a single cron job, add the information supplied by your host (see above screenshot) to redirect the command’s output to /dev/null. (e.g. /usr/local/cpanel/bin/is_script_stuck /dev/null 2>&1)

How to Edit a Cron Job

To edit your cron job settings, go to the Current Cron Jobs section and click the Actions > Edit link.

Edit cron job.Edit cron job.

Change the values in the text boxes and click the Edit Line button to update your settings.

Edit cron job settings.Edit cron job settings.

Your cron job settings are now updated.

Updated cron job settings.Updated cron job settings.

How to Delete a Cron Job

To delete a cron job, go to the Current Cron Jobs section and click the Actions > Delete link.

Delete cron job.Delete cron job.

Confirm your deletion by pressing the Delete button.

Confirm cron job deletion.Confirm cron job deletion.

The selected cron job will be removed from the Current Cron Jobs section.

No cron jobs.No current cron jobs.

Useful Tips and Additional Information on Using Cron Jobs

Here are some additional tips and useful information on using cron jobs:

If Cron Jobs Are Not Available in Your cPanel Dashboard

Some hosts do not allow users to run cron jobs. If your host has not made cron jobs available in your cPanel dashboard, you can still create and run cron jobs using external services like the ones listed below:

How to Check if Your Cron Job Has Run

Some cron jobs produce output (e.g. a file, email, or web page) and others do not. It is easier to check if the cron ran for jobs that produce output.

For cron jobs that perform tasks behind the scene and don’t produce output, you will need to have root access to your server to check the logs and determine if the job has run as scheduled.

You can check your server logs using an SSH client. Your host should provide the command to check the log for the cron script and the output will normally include the date and time the cron job has run, plus the server name, cron ID, cPanel username, and the command that ran. The name of the script should display at the end of the command.

How to Avoid Cronflicts

cPanel recommends exercising caution when scheduling cron jobs. You should allow enough time between cron jobs for the previous cron job to complete. If you schedule them to run too often, the server could start another cron job before the last cron job ends. This duplication can slow down your server and degrade its performance.

Additionally, many hosts recommend not setting crons to run at regular intervals smaller than 5 minutes apart (some hosts won’t even allow you to run them less than 15 minutes apart).

WordPress Cron (WP-Cron)

In addition to running cron jobs from your hosted server, WordPress also has an internal built-in function it uses to run scheduled tasks on WordPress sites, called WordPress Cron (WP-Cron, wp-cron.php).

WordPress core and many plugins need a scheduling system to perform time-based tasks. These rely on WP-Cron to run backend functions like checking for updates, creating backups, deleting old comments from trash, etc.

Many hosting services are shared, however, and do not provide access to the system scheduler. WP-Cron allows WordPress to run on just about any hosting environment.

Note: you can view and control cron events in the WP-Cron system using a plugin like WP Crontrol.

How To Configure a Real Cron For WordPress

Although the WordPress built-in Cron handler is a really useful feature, WP-Cron is not a real cron job. It only simulates what a system cron does. However, WP-Cron does not run continuously. It is only triggered whenever someone visits your website.

This can cause performance issues for both high-traffic and low-traffic sites, especially if there are critical tasks that must run on time. On high-traffic sites, wp-cron.php fires on every page load. If a site doesn’t have enough freed-up PHP resources, the cron has to wait. On low-traffic sites, cron schedules can be missed if not enough frequent users visit the site to load a page.

A better approach is to disable WP-Cron and use the system cron instead. This ensures that cron will run on a pre-defined schedule, on the intervals (or at the specific time) you desire, and prevents WordPress from continuing to run WP-Cron on each page load, which is unnecessary and contributes to extra resource usage on your server.

To disable WP-Cron, open the wp-config.php file for editing and add the following before the “That’s all, stop editing! Happy publishing.” line:

define('DISABLE_WP_CRON', true);

Disable WP-Cron.Disable WP-Cron.

Note: This disables WP-Cron from running on page load, not when called directly via wp-cron.php.

Next, set up your system’s task scheduler to make a web request to the wp-cron.php file by creating a cron job and adding the following command (remember to replace yourdomain.com with your actual domain):

wget -q -O - http://yourdomain.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1

An easier option is to host your WordPress sites with WPMU DEV. We automatically run WP-Cron every 5 minutes. This guarantees that all functions on your WordPress site(s) will run exactly as scheduled. And, of course, we provide expert 24/7 support if additional help is needed.