How-to

How to Set Up a Cron Job in cPanel

Server rack illustration with a Cron job clock tile, titled How to set up a cron job in cPanel

This guide schedules a command in the Cron Jobs interface of cPanel (version 82 and later), so that it runs on its own at set times: a PHP script, for example, or a request to a URL. It covers the cron email, the five time fields and how to read them, the command itself, and how to edit or delete a job.

Before you start

Every LyraHosting web hosting account is managed with cPanel. To find the scheduler, open cPanel's Advanced section, then Cron Jobs. If it isn't there, open a support ticket. Before you add a job, have these ready:

  • the full path of the script or program to run, for example /home/username/public_html/cron.php, where username is your cPanel username. cPanel asks for absolute paths;
  • a way to see errors. SSH access is included on SSD Enterprise only, so on SSD Starter you can't try the command in a shell first. Set the cron email below to see what a job prints.

Don't schedule a job so often that a new run starts before the last one has finished: overlapping runs can slow the server down. Take great care with rm in a cron job, because the wrong options can delete data in your home directory.

Set the cron email

The Cron Email section at the top of the page holds the address for job notifications. Cron mails anything a job prints, including error messages, to this address. Enter an email address and click Update Email. To stop the email for all jobs, remove the address. To stop it for one job only, see the section on silencing a job below.

Add a cron job

  1. In the Common Settings menu, choose an interval, for example Once Per Hour. cPanel fills in the five time fields for you. You can also type your own values into Minute, Hour, Day, Month and Weekday.
  2. In the Command box, enter the command, with the full path to the program and to the file it works on.
  3. Click Add New Cron Job. The job appears in the Current Cron Jobs table.

All five time fields and the command need a value. If the command runs a script file directly, such as a shell script, that file needs execute permission.

How to read the cron syntax

The five time fields, from left to right, say when the job runs:

  • Minute: 0 to 59;
  • Hour: 0 to 23;
  • Day, the day of the month: 1 to 31;
  • Month: 1 to 12;
  • Weekday: 0 to 7, where both 0 and 7 mean Sunday.

An asterisk means every value. A hyphen gives a range (1-5), commas give a list (1,15), and a slash gives a step: */15 in the Minute field means every 15 minutes. Some examples:

0 * * * *      every hour, on the hour
*/15 * * * *   every 15 minutes
30 2 * * *     every day at 02:30
0 3 * * 1      every Monday at 03:00
0 4 1 * *      at 04:00 on the 1st of every month

Three details catch people out. If you restrict both Day and Weekday, the job runs when either one matches, so 0 4 1 * 1 runs on the 1st of the month and on every Monday. A step only counts within its own field: */35 in Minute runs at :00 and :35 past each hour, not every 35 minutes. And cron reads a % sign in the command as a new line, so write it as \%, for example in a date format or a URL.

Run a PHP script

Run a PHP script with the PHP command-line binary, and give full paths to both:

/usr/local/bin/php /home/username/public_html/cron.php

Our hosting servers run CloudLinux. If your cPanel has a Select PHP Version page, in the Software section, /usr/local/bin/php in a cron job runs the PHP version you chose there. To run one specific installed version instead, call its own command-line binary, which CloudLinux keeps under /opt/alt/, for example /opt/alt/php83/usr/bin/php for PHP 8.3. If the cron email says a command wasn't found, open a support ticket and ask for the right path.

Call a URL with curl or wget

Some applications give you a URL to call on a schedule rather than a script to run. To request it with curl:

curl -fsS -o /dev/null https://example.com/cron.php

-s hides the progress output, -S still prints an error message if the request fails, -f treats an HTTP error such as 404 or 500 as a failure, and -o /dev/null discards the page that comes back. The job then prints something, and you get an email, only when the request goes wrong.

To use wget instead:

wget -q -O /dev/null https://example.com/cron.php

-q turns off wget's output, and -O /dev/null discards the download. Without -O, wget saves a copy of the page on every run, as cron.php, then cron.php.1, cron.php.2 and so on. Unlike the curl command above, this one also stays quiet when the request fails.

Silence the email for one job

When a job works and you don't want its output by email, send the output to /dev/null. Click Edit next to the job and add this to the end of the command:

>/dev/null 2>&1

For example:

/usr/local/bin/php /home/username/public_html/cron.php >/dev/null 2>&1

>/dev/null discards the normal output, and 2>&1 sends error messages to the same place. Keep this order: with 2>&1 first, errors would still reach your inbox. And don't leave out the > sign: without it, the shell passes /dev/null to your command as one more argument, and nothing is silenced.

Edit or delete a cron job

Your jobs are listed in the Current Cron Jobs table. To change one, click Edit next to it, change the time fields or the command, and click Edit Line. To remove one, click Delete next to it, then click Delete again to confirm.

Host it offshore with LyraHosting

cPanel hosting on SSD in the Netherlands with free SSL and daily backups, from €5.99 a month.