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.
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 Jobs
Select 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 Settings
Cron – 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 command
Type or paste in the cron job command.

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

Current cron jobs
Your 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 Email
Get 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.

On high-traffic sites, WP-Cron is not an issue. As pointed out in the comments section of this post by one of our members (DavidAnderson of UpdraftPlus.com):

  • The code in WordPress core is invoked on page loading only if there is an overdue cron job (i.e. only if needed).
  • In a high traffic site, a cron job will run precisely on schedule. If you switch to an external cron caller, your cron job will only run when that schedule is hit.
  • High-traffic sites tend to be more complicated and so have more cron jobs. On WP-Cron, jobs run precisely when needed. Switching to an external cron vastly increases the chances of your cron jobs not running on time, as they will now only run to whatever frequency you set their task (every 10 minutes, 5 minutes, 1 minute, etc.).

On low-traffic sites, however, cron schedules can be missed if not enough frequent users visit the site to load a page.

While experts from both our team and member community maintain that you should not disable WP-Cron and use an external cron scheduler unless you know exactly what you are doing, if you are ever asked to disable WP-Cron, here’s how to do it:

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.

You can then, 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

One last point to mention here (once again, thanks to DavidAnderson for raising this):

The only real case for moving to an external cron is if you are on a very low-traffic site, you can’t schedule external visits for some reason (e.g. your site is password protected), and it’s important that those jobs run on time. Otherwise, just leave it alone and all will be well.

We agree with this. Hence, if your sites run on WPMU DEV hosting, we automatically run WP-Cron every 5 minutes. Having a default preset cron helps low traffic sites, as it guarantees scheduled tasks to run every 5 minutes at least.

Finally, as DavidAnderson also points out, if you have a lot of cron jobs, then even every 1 minute may not be enough to process them all. A number of popular plugins have jobs that are scheduled to run every minute. If those take significant time, they can delay others from running.

Cronfused Yet?

Don’t be! And if this seems too cronplicated, don’t worry. Fortunately, our team is here to provide 24/7 expert help and support with anything you need.

Free Video Why 100 is NOT a Perfect Google PageSpeed Score (*5 Min Watch) Learn how to use Google PageSpeed Insights to set realistic goals, improve site speed, and why aiming for a perfect 100 is the WRONG goal.
Martin Aranovitch
Martin Aranovitch is a WordPress trainer and educator and the author of several WordPress guides and courses. Martin believes most problems can be solved with a WordPress plugin.
We hope that you have found this tutorial useful. Questions? Comments? Please share them below.