Explore WaseerHost

Appearance

GuidecPanel 11 min read

How to Set Up a Cron Job in cPanel (With Examples)

Create a cron job in cPanel step by step. Learn the five-field schedule syntax, PHP, WordPress and Laravel commands, email output and how to test a job.

How to Set Up a Cron Job in cPanel (With Examples)

Summarize with:

Share:

A cron job in cPanel is a command that the server runs for you on a schedule, such as every five minutes or once a night. You create one in the Cron Jobs interface by choosing a schedule in five time fields and typing the command to run. This guide covers the schedule syntax, working commands for PHP, WordPress and Laravel, how to test a job, and the mistakes that stop most cron jobs from running.

Key takeaways

  • Open Cron Jobs in the Advanced section of cPanel, pick a schedule, enter a command with absolute paths, and click Add New Cron Job.
  • A schedule has five fields in this order: minute, hour, day of month, month, weekday. An asterisk means "every".
  • On cPanel servers, /usr/local/bin/php runs PHP scripts from the command line with the PHP version set for that folder.
  • Cron emails you whatever a job prints. Send the output to a log file while you test, then to /dev/null once the job works.
  • Most failed jobs come down to a relative path, the wrong PHP binary, an unescaped % sign or a schedule in the wrong time zone.

What a cron job in cPanel does

Cron is the scheduler built into Linux servers. It checks its list of jobs once a minute and runs every command whose schedule matches the current time. cPanel puts a form on top of it, so you don't have to edit the crontab file by hand.

Typical uses on a hosting account are sending queued email, running WordPress scheduled tasks on time, running the Laravel scheduler, importing a product feed and clearing old cache files.

Each cPanel account has its own crontab, and cron runs the commands as that account's user. A job has the same file permissions as your account.

Your hosting provider can switch the Cron Jobs interface on or off, according to the cPanel documentation. If you can't find it, ask your host. Every WaseerHost hosting plan uses cPanel.

Cron schedule syntax

Every cron job starts with five time fields, followed by the command. The crontab manual defines the allowed values.

FieldcPanel labelAllowed values
MinuteMinute0 to 59
HourHour0 to 23
Day of monthDay1 to 31
MonthMonth1 to 12, or names such as jan
Day of weekWeekday0 to 7, where 0 and 7 are both Sunday, or names such as mon

Four symbols cover almost every schedule:

  • * matches every value in the field.
  • , makes a list. 1,15 in the day field means the 1st and the 15th.
  • - makes a range. 1-5 in the weekday field means Monday to Friday.
  • / sets a step. */5 in the minute field means every fifth minute.

Here are schedules you can copy into the five fields.

ScheduleWhen it runs
* * * * *Every minute
*/5 * * * *Every 5 minutes
0 * * * *Once an hour, on the hour
0 */2 * * *Every two hours
0 2 * * *Every day at 02:00
0 22 * * 1-5Monday to Friday at 22:00
30 4 * * 0Every Sunday at 04:30
0 0 1 * *At midnight on the first day of each month

Two rules in the manual are easy to miss.

A step only counts inside its own field. */35 in the minute field does not mean "every 35 minutes". It runs at minute 0 and minute 35 of every hour.

If you fill in both the day of month and the weekday, cron runs the job when either one matches. The manual's example is 30 4 1,15 * 5, which runs at 04:30 on the 1st and 15th of each month and also on every Friday.

How to add a cron job in cPanel

These steps follow the cPanel documentation for the Cron Jobs interface.

  1. Log in to cPanel and open Cron Jobs in the Advanced section.
  2. Under Cron Email, enter the address that should receive the output of your jobs and click Update Email.
  3. Choose an interval from the Common Settings menu, for example Once Per Hour(0 * * * *). cPanel fills in the Minute, Hour, Day, Month and Weekday boxes for you. You can also type your own values into each box.
  4. In the Command box, enter the command with its absolute path.
  5. Click Add New Cron Job. The job now appears in the Current Cron Jobs table.

To change a job later, click Edit next to it in the table, change the values and click Edit Line. To remove it, click Delete and confirm.

cPanel's own channel has a short video about this interface.

Cron commands for PHP, WordPress and Laravel

In the examples below, replace user with your cPanel username and adjust the folder names to match your site.

Run a PHP script

Call the PHP binary first, then give the full path to the script.

bash
/usr/local/bin/php /home/user/public_html/cron/send-reports.php

cPanel's ea-php-cli documentation explains that /usr/local/bin/php calls the command-line version of PHP and picks the PHP version configured for the script's folder. To force a version, the same page lists version-specific binaries such as /usr/bin/ea-php81. Some hosts use a different PHP setup with other paths, so confirm the path with your host if the command fails.

On WaseerHost you can select PHP 8.0 to 8.5 per site in cPanel. See PHP hosting for details.

Run WordPress cron on a real schedule

WordPress has its own scheduler, WP-Cron, which only fires when someone loads a page. On a quiet site, scheduled posts and backups can run late. The WordPress developer handbook describes the fix in two parts.

First, switch off the page-load trigger by adding this line to wp-config.php:

php
define( 'DISABLE_WP_CRON', true );

Then add a cron job that requests wp-cron.php. For a run every 15 minutes, set the schedule to */15 * * * * and use the handbook's command with your own domain:

bash
wget --delete-after https://example.com/wp-cron.php

The --delete-after option stops wget from saving the response as a file in your account. If you are still choosing a host for the site, WaseerHost WordPress hosting runs on cPanel, so these steps apply as written.

Run the Laravel scheduler

Laravel needs one cron entry that runs every minute. The framework then decides which of your scheduled tasks are due. The Laravel scheduling documentation gives this entry:

bash
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

In cPanel, put * in all five time boxes and enter only the command part, with real paths:

bash
cd /home/user/myapp && /usr/local/bin/php artisan schedule:run >> /dev/null 2>&1

Laravel apps run in cPanel on WaseerHost Laravel hosting. The guide to deploying Laravel on cPanel covers the rest of the setup.

Control cron email output

Cron sends an email when a job prints anything, including errors. That helps while you set a job up. It becomes a problem when a job that runs every minute fills the inbox.

You have three options.

To keep the emails, leave an address in Cron Email.

To stop the emails for one job, discard its output by ending the command with a redirect to /dev/null:

bash
/usr/local/bin/php /home/user/public_html/cron/send-reports.php >/dev/null 2>&1

To stop the emails for all jobs, remove the address from Cron Email, as the cPanel documentation describes.

A log file is often the better choice. The job below appends everything it prints to a file you can open in File Manager:

bash
/usr/local/bin/php /home/user/public_html/cron/send-reports.php >> /home/user/logs/reports.log 2>&1

Create the logs folder first. Cron won't create it for you.

How to test a cron job

Don't wait until 02:00 to find out whether a nightly job works. Test it in three short steps.

  1. Prove that cron runs at all. Add a job with the schedule * * * * * and this command, then check after two minutes that the file exists and gains a new line each minute.
bash
echo "cron ran" >> /home/user/cron-test.log
  1. Run your real command by hand. If your host enables Terminal in cPanel or SSH access, paste the exact command there and read the output. A command that fails in the terminal will also fail in cron.
  2. Run the real command from cron every minute with its output going to a log file, as shown in the previous section. When the log shows a clean run, change the schedule to the one you want and delete the test job.

Remember to remove the every-minute test. cPanel's documentation warns that jobs scheduled too close together can start again before the previous run ends, which slows the server down.

Common cron job mistakes in cPanel

Relative paths

Cron does not start in your script's folder. Write /home/user/public_html/cron/task.php, not cron/task.php. Inside a PHP script, build file paths from __DIR__ for the same reason.

Calling the script without PHP

A command of just /home/user/public_html/task.php asks the server to execute the file directly. That only works if the file has execute permission and a first line that names its interpreter. Putting /usr/local/bin/php in front avoids both requirements.

Percent signs in the command

Cron treats % as a line break and passes everything after it to the command as input. A command such as date +%Y-%m-%d breaks without warning. Escape each one with a backslash, as in date +\%Y-\%m-\%d.

The wrong time zone

Cron compares the schedule with the server's clock, and your server may be far from where you live. WaseerHost servers, for example, are in data centers in the USA, Germany and Finland. Run date in Terminal to see the server time before you set a job for "midnight". The manual also notes that a job set for an hour that daylight saving time skips will not run that day.

Risky commands

The cPanel documentation tells you to take extreme care with rm in a cron job, because the wrong options can delete your home directory's data. Test any command that deletes files by hand first, with full paths.

If the job still doesn't run

Work through this list in order.

  1. Check that the job appears in the Current Cron Jobs table with the schedule you expect.
  2. Point the output at a log file and read the error. "No such file or directory" means a wrong path. "Permission denied" means the script or folder permissions block your user.
  3. Confirm the PHP path with your host if the log says php was not found.
  4. If the same script also fails when you open it in a browser, work through the 500 Internal Server Error checklist to find the PHP error.

Frequently asked questions

Log in to cPanel and look in the Advanced section for Cron Jobs. If the icon is missing, your hosting provider has disabled the feature for your plan, and only the provider can turn it back on.

Haseeb Raza

Written by

Haseeb Raza

Web & AI Systems Lead

Web and AI Systems Lead at Wasevo, the company behind WaseerHost. He builds web applications and AI systems.

Related Guides

Get new guides by email

One short email when we publish something useful. No spam; unsubscribe at any time.

Free SSL · Free migration · 24/7 support

cPanel hosting

See hosting plans
30-day money-back guarantee

Renews at the same price for the term you choose