A cron job runs a command on your hosting account automatically on a schedule - sending a nightly report, clearing a cache, triggering a CMS's scheduled tasks, or pulling in a data feed. cPanel's Cron Jobs tool, under Advanced, sets them up. This guide covers the syntax, the common recipes, and the mistakes that turn a helpful cron job into the thing that takes your site offline.

Frequency is the whole game. Every cron run consumes an entry process, CPU and memory from your account's allocation. A job scheduled every minute that only needs to run hourly generates 60 times the load for no benefit, and is a genuinely common cause of 508 errors. Choose the longest interval that still does the job.

Creating a cron job

  1. Log in to cPanel from My Products & Services.
  2. Under Advanced, open Cron Jobs.
  3. Set Cron Email to an address you actually read. Output and errors are mailed there, and it is how you find out a job is failing.
  4. Under Add New Cron Job, pick an interval from Common Settings, or set the five time fields yourself.
  5. Enter the Command to run.
  6. Click Add New Cron Job.

Understanding the schedule fields

The five fields are, in order: minute, hour, day of month, month, day of week.

*  *  *  *  *
│  │  │  │  └── day of week   (0-6, Sunday = 0)
│  │  │  └───── month         (1-12)
│  │  └──────── day of month  (1-31)
│  └─────────── hour          (0-23)
└────────────── minute        (0-59)
Schedule Means
*/15 * * * *Every 15 minutes
0 * * * *Every hour, on the hour
30 3 * * *Every day at 03:30
0 2 * * 1Every Monday at 02:00
0 0 1 * *First day of every month at midnight
0 9-17 * * 1-5Hourly, 09:00–17:00, Monday to Friday
Avoid round numbers. Schedule at 03:07 rather than 03:00. Everybody picks the top of the hour, so staggering your jobs by a few minutes means yours is not competing with everyone else's for disk throughput.

Common commands

Run a PHP script

/usr/local/bin/php /home/username/public_html/scripts/task.php

Use the absolute path to the file, and replace username with your cPanel username - your home directory is shown on the cPanel home screen under General Information.

Run a script with a specific PHP version

/opt/cpanel/ea-php84/root/usr/bin/php /home/username/public_html/scripts/task.php

Useful when the domain runs a different version from the system default.

Trigger WordPress scheduled tasks properly

By default WordPress only runs its scheduled tasks when somebody visits the site, which is unreliable on a quiet site and wasteful on a busy one. Replace it with a real cron job:

  1. Add this line to wp-config.php, above the "stop editing" comment:
    define('DISABLE_WP_CRON', true);
  2. Add a cron job every 15 minutes:
    /usr/local/bin/php /home/username/public_html/wp-cron.php >/dev/null 2>&1

Fetch a URL

curl -s https://yourdomain.com/cron/task >/dev/null 2>&1

Back up a database nightly

mysqldump -u dbuser -p'dbpassword' dbname > /home/username/backups/db-$(date +\%F).sql

Note the escaped \%. In cron, an unescaped % is treated as a newline and the command silently breaks - one of the most confusing cron behaviours there is. Pair this with a second job that deletes files older than 14 days, or it will fill your disk.

Controlling the email output

By default cron emails you everything a job writes to output. A chatty job running every 15 minutes will bury you.

Append to the command Effect
(nothing)Email on every run that produces output
>/dev/nullSuppress normal output, still email errors - usually the right choice
>/dev/null 2>&1Suppress everything, including errors
>>/home/username/cron.log 2>&1Log to a file instead of email - best for anything you may need to debug
Silencing errors hides failures. A job with 2>&1 that has been broken for three months looks exactly like one that is working. Log to a file rather than discarding, and check it occasionally.

Troubleshooting

Symptom Cause and fix
Job never seems to run Almost always a relative path. Cron does not run from your home directory - use absolute paths for the interpreter and the script.
"No such file or directory" Check the path character for character in File Manager. Linux paths are case sensitive.
Works in the browser, fails in cron CLI PHP is a different environment from web PHP - different settings, no web server variables, no session. Scripts relying on $_SERVER need adapting, or fetch the URL with curl instead.
Command stops at the date Unescaped %. Escape every one as \%.
Site slow at predictable intervals Your own cron. Reduce frequency, move it to quiet hours, and check Metrics → Resource Usage to confirm the timing matches.
Overlapping runs piling up The job takes longer than its interval, so copies accumulate until processes are exhausted. Lengthen the interval, or add a lock file so a second copy exits immediately.
Flooded with cron emails Append >/dev/null, or clear the Cron Email field to stop notifications entirely.
Ran at the wrong time Cron uses the server's timezone, which may not be yours. Check the server time under Server Information on the cPanel home screen and offset accordingly.
Disk filling up A backup or log job with no cleanup. Add a companion job: find /home/username/backups -name "*.sql" -mtime +14 -delete

Frequently asked questions

What is the shortest interval I can use?

Once per minute is possible, but rarely wise on shared hosting. Every run consumes an entry process. Fifteen minutes suits most jobs, and hourly suits most of the rest.

How do I know a job actually ran?

Leave the Cron Email set while you are testing, or redirect output to a log file and check it. A job that produces no output and sends no mail gives you no evidence either way.

Where do I find the PHP path?

/usr/local/bin/php uses the domain's configured version. For a specific version, use /opt/cpanel/ea-phpXX/root/usr/bin/php, replacing XX with 82, 83, 84 or 85.

Can a cron job run something outside my account?

No. Cron jobs run as your cPanel user and are confined to your own account, which is exactly as it should be.

Should I use cron or my application's own scheduler?

Use a real cron job. Application schedulers that depend on visitor traffic are unreliable on quiet sites and add overhead on busy ones.

Will a cron job keep running if my site is suspended?

No. Suspension stops the account, cron jobs included. They resume once the account is reactivated.

Cron job not firing? Open a ticket with Technical Support at My Support Tickets with the exact command line, the schedule, and any output you received. Our engineers can check the system cron log and tell you whether it ran and what it returned.
Hai trovato utile questa risposta? 0 Utenti hanno trovato utile questa risposta (0 Voti)

Powered by WHMCompleteSolution