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

[…]

 



This article was written by Martin Aranovitch and originally published on WPMU DEV Blog.

Disclosure: Some of the links in this post are "affiliate links." This means if you click on the link and purchase the product, We may receive an affiliate commission.

Leave a Comment

You have to agree to the comment policy.

Scroll to Top