Setting up a cPanel cron job: a guide to automated tasks

8 min read

Behind the scenes of a website, there are many recurring tasks: sending out newsletters, publishing scheduled posts, synchronising online shop stock, deleting temporary files, backups and reports. You don’t need to run these manually: cron is the scheduler for Linux servers, which automatically runs a command or script at specified times. On cPanel hosting plans, you can configure this via the graphical interface in the Cron Jobs menu.

In this guide, we explain how cron works and how to read the scheduling; we’ll walk you through creating a task in cPanel step by step; we’ll provide practical command examples; we’ll show you how to replace WordPress’s built-in scheduler; and we’ll summarise common mistakes.

What is cron used for?

  • WordPress: scheduled posts, plugin tasks, WooCommerce operations (see below for replacing wp-cron).
  • Online shop: stock and price synchronisation with the supplier, product feed generation, abandoned basket emails.
  • Maintenance: deleting logs and temporary files, database clean-up, clearing the cache.
  • Backup: the database backup is saved to a file, which you can then download or forward.
  • Reports and notifications: daily summaries, alerts.
  • Framework schedulers: Laravel, Symfony and many other frameworks base their own schedulers on a single cron job that runs every minute.

Reading cron schedules

A cron entry consists of five time fields and a command:

*  *  *  *  *  parancs
│  │  │  │  └── day of the week (0–7, Sunday is 0 or 7)
│  │  │  └───── month (1–12)
│  │  └──────── day of the month (1–31)
│  └─────────── hour (0–23)
└────────────── minute (0–59)
  • * = all values
  • */15 = every 15th (for example, every 15 minutes)
  • 1,15 = list
  • 1-5 = range (for example, Monday to Friday)
Timing Meaning
*/15 * * * * Every 15 minutes
0 * * * * At the start of every hour
30 2 * * * Every day at 2.30
0 6 * * 1-5 At 6 am on weekdays
0 3 * * 0 At 3 am on Sunday
0 0 1 * * At midnight on the first day of every month

The times are given in the server’s time zone. For more details on the syntax, see the article explaining crontab syntax; if you’re unsure, you can enter a schedule on the crontab.guru website to see what it means in plain text.

Creating a cron job in cPanel

  1. Log in to cPanel and, in the ‘Advanced’ section, open the ‘Cron Jobs’ menu item.
  2. Email address (Cron Email): enter the address to which the system should send the task output. This is useful during setup, but it’s worth muting it afterwards (see below).
  3. Add New Cron Job: you can select a pre-set schedule from the ‘Common Settings’ drop-down list (for example, ‘Once Per Day’), or you can fill in the five fields manually.
  4. Command: add the command to be run (see the examples).
  5. Click the ‘Add New Cron Job’ button. The task will appear in the list below, where you can edit or delete it later.

The article on creating a cron job in cPanel also illustrates the steps with images.

Video: cPanel Tutorials - Cron Jobs (cPanelTV)

Command examples

Running a PHP script (this is the best method, as it runs directly, bypassing the web server):

/usr/local/bin/php /home/felhasznalonev/public_html/scripts/feladat.php

Replace felhasznalonev with your cPanel username; you can see the exact path in File Manager. On aWh’s cPanel hosting plans, the /usr/local/bin/php directory runs the PHP version set for your account in the ‘Select PHP Version’ menu.

Calling a URL (if the task is triggered by a web address and cannot be accessed directly as a file):

curl -s https://cegnev.hu/cron-feladat > /dev/null 2>&1

or wget -q -O /dev/null https://cegnev.hu/cron-feladat. This is slower and less reliable than running PHP directly, as it goes through the web server and is subject to its time limits.

WP-CLI command (if the package includes SSH and WP-CLI):

cd /home/felhasznalonev/public_html && wp cron event run --due-now > /dev/null 2>&1

Laravel scheduler:

cd /home/felhasznalonev/projekt && /usr/local/bin/php artisan schedule:run > /dev/null 2>&1

Replacing WordPress’s wp-cron

WordPress does not use the server’s cron, but rather its own ‘pseudo-cron’ system: every time a page is loaded, it checks whether there are any tasks due, and if so, it runs them. This has two drawbacks: on low-traffic sites, tasks are delayed (if nobody visits the site, they do not run), whilst on high-traffic sites, it causes unnecessary load and slows the site down.

The solution: disable the built-in timer and run it via the server’s cron.

  1. In the wp-config.php file, just before the line ‘That’s all, stop editing!’, enter:

    define( 'DISABLE_WP_CRON', true );
    
  2. Create a cron job in cPanel, for example, to run every 5 or 15 minutes:

    /usr/local/bin/php /home/felhasznalonev/public_html/wp-cron.php > /dev/null 2>&1
    

For online shops (WooCommerce), a 5-minute frequency is a good starting point; for simple blogs, even a 15-minute interval is sufficient. The detailed steps are set out in the article on replacing the WP-Cron cPanel cron job.

Handling the output

By default, cron sends the output of every run to the specified email address. This is useful during setup, but for a task running every 5 minutes, it would mean 288 emails a day. Once the task is working properly, add the following to the end of the command:

> /dev/null 2>&1

If you’d like to save the output, redirect it to a file (>> /home/felhasznalonev/logs/cron.log 2>&1), and clear the log from time to time to prevent your storage space from filling up.

Troubleshooting: if the task does not complete

  • Incorrect path: cron does not run from the public_html folder, so always specify the full, absolute path to the script (and if the script uses relative paths, use cd to navigate to its folder first).
  • Incorrect PHP: if the command is simply php, it may not run the expected version; use the full path.
  • Permissions: the script must be readable by your user; if you are running an executable file (shell script) directly, it must also have execute permissions.
  • Timing: check that the five fields are in the correct order and that you’ve taken the server’s time zone into account.
  • The script itself is faulty: run it manually via SSH or in your browser and check the error message; temporarily re-enable the sending of output via email.
  • Resource limits: on shared hosting, cron jobs also run within the account’s CPU and memory limits. A heavy, long-running task may reach the limit or slow down the website; our knowledge base article explains how cron and SSH are factored into resource limits.

Best practices

  • Don’t run everything every minute. Think carefully about how often the task really needs to run; running it too frequently puts a strain on your account’s resources.
  • Spread the start times out: don’t have every task start on the hour; choose 2:17, for example.
  • For long-running tasks, make sure it doesn’t restart whilst the previous one is still running (for example, using a lock file or the flock command).
  • Do not include your password in the command or the URL; store your secrets in the script’s configuration file, outside the web root.
  • Protect tasks that can be launched via a URL with a secret key or IP restriction to prevent them from being invoked at will from outside the network.

Summary

Cron is a reliable scheduler for recurring tasks on websites. In cPanel, it can be set up in just a few clicks via the Cron Jobs menu; always use the full path in your commands, mute the output of running tasks, and for WordPress, replace the built-in wp-cron with the server’s cron. On aWh’s cPanel hosting plans, cron jobs are available with every plan.

Frequently Asked Questions

How often can I run a cron job?

The smallest unit in cron is one minute. On shared hosting, however, you should only run tasks every minute if it is absolutely necessary; for most tasks, a frequency of every 5–15 minutes or once an hour is sufficient.

How do I know that the task has finished running?

Temporarily leave email notifications enabled, or redirect the output to a log file and view it in File Manager.

What is the difference between PHP and the wget/curl commands?

The php command runs the script directly, bypassing the web server: it’s faster and isn’t subject to web time limits. wget and curl call a web page address as if a visitor were opening it; use them if the task can only be initiated via a URL.

Which PHP path should I use?

On aWh’s cPanel hosting plans, the path /usr/local/bin/php runs the PHP version configured for your account.

Can cron slow down a website?

Yes, if a resource-intensive task runs frequently, as it shares resources with the website. Schedule resource-intensive tasks for periods of low traffic, and avoid running them unnecessarily often.

Do you need cron for WordPress?

It’s not essential, as WordPress works with its built-in scheduler. However, for high-traffic websites and online shops, it’s worth switching to the server’s cron job, as it’s more reliable and puts less strain on the site.

Back to the blog