Skip to content
Cron Cookbook

How to run a PHP script as a cron job

To run a PHP script on a schedule, add a line to your crontab with crontab -e: the five schedule fields, then the full path to php and to the script, as in */5 * * * * /usr/bin/php /var/www/app/cron.php. Cron runs the PHP command line, not your web server, so the script gets a different environment from the one you test in the browser.

Five things that trip up PHP cron jobs

  1. Cron needs the full path to php

    Cron's PATH is short. Find the binary with command -v php and write it out, along with the script's full path.
  2. The CLI reads its own php.ini

    Settings and extensions you see in phpinfo() on the web may differ from what the command line loads. Check with php --ini.
  3. The working directory is your home

    Relative paths in require or fopen break. Build them from __DIR__.
  4. Errors vanish unless you log them

    Redirect the output to a file, or cron mails it to an inbox nobody reads.
  5. A slow run can overlap the next

    On the command line max_execution_time defaults to 0, so nothing stops a stuck script. Wrap it in flock.

Add the job

First run the script by hand from the command line, exactly as cron will: /usr/bin/php /var/www/app/cron/send-emails.php. If it fails there, it will fail in cron. Then open your crontab with crontab -e, add the line and save. The job runs as the user whose crontab it is, so pick the user that owns the files the script writes, often www-data: sudo crontab -u www-data -e.

On shared hosting with cPanel or Plesk, there's often no shell: the control panel asks for the schedule and the command separately. The command is the same, with the PHP path your host uses, as the next section shows for cPanel.

Set up a PHP cron job in cPanel

In cPanel, open Cron Jobs (in the Advanced section). Under Add New Cron Job, either pick a schedule from Common Settings, which fills in the five fields for you, or set Minute, Hour, Day, Month and Weekday yourself; each box takes the same values as that field of a crontab line. Then paste the command and click Add New Cron Job. cPanel writes the line to your account's crontab, so everything else on this page still applies.

The command needs full paths. Your files live under /home/username (cPanel's General Information panel shows the exact home directory), and the server's default PHP is usually /usr/local/bin/php. The Command box takes a single line, so paste one command per job:

cPanel › Cron Jobs › Command
/usr/local/bin/php /home/username/public_html/cron/send-emails.php >> /home/username/logs/send-emails.log 2>&1

That default can be a different version from the one MultiPHP Manager sets for your site. On servers with EasyApache 4 each version has its own binary, so to run the job on PHP 8.3 whatever the server's default is, paste this command instead:

cPanel › Cron Jobs › Command (PHP 8.3)
/opt/cpanel/ea-php83/root/usr/bin/php /home/username/public_html/cron/send-emails.php >> /home/username/logs/send-emails.log 2>&1

Cron Email, at the top of the page, is where cPanel mails anything a job prints, for every job in the account. A job that runs every few minutes fills that inbox, which is why the commands above append their output to a log file instead. To throw the output away, end the command with >/dev/null 2>&1, but then errors disappear too. Create the logs folder first: cron won't.

The Common Settings presets are ordinary cron expressions, and all of them fire at minute 0, so on a shared server many accounts' jobs start in the same minute. Moving yours off minute 0 spreads the load, as long as it keeps the schedule. For presets that run once an hour or less often, change the Minute box to an odd value such as 17. For the shorter ones, shift every run by the same amount instead, as the last column shows: 17 alone in the Minute box of Once Per Five Minutes runs the job once an hour, not twelve times.

Common SettingsExpressionRunsOff minute 0
Once Per Minute* * * * *Every minute.Runs every minute
Once Per Five Minutes*/5 * * * *Every 5 minutes.2-59/5 * * * *
Twice Per Hour0,30 * * * *Every 30 minutes.17,47 * * * *
Once Per Hour0 * * * *Every hour, on the hour.17 * * * *
Twice Per Day0 0,12 * * *Every 12 hours from 00:00 through 12:00.17 0,12 * * *
Once Per Day0 0 * * *At 00:00 every day.17 0 * * *
Once Per Week0 0 * * 0At 00:00 on Sundays.17 0 * * 0
On the 1st and 15th of the Month0 0 1,15 * *At 00:00 on the 1st and 15th of every month.17 0 1,15 * *
Once Per Month0 0 1 * *At 00:00 on the 1st of every month.17 0 1 * *
Once Per Year0 0 1 1 *At 00:00 on the 1st of January.17 0 1 1 *

Hosts set their own limits

Many shared hosts don't allow jobs more often than every 5 or 15 minutes, and stop scripts that run too long. The times are in the server's time zone, which may not be yours. In Plesk the same page is Scheduled Tasks, where the task type Run a PHP script lets you pick the PHP version from a list.

Use the full path to php

Cron doesn't read your shell profile, and its PATH is short (Debian's cron uses /usr/bin:/bin), so a php installed in /usr/local/bin or by a version manager isn't found. Run command -v php in your terminal and copy the result into the crontab. A server with several PHP versions has a binary for each, such as /usr/bin/php8.3; name the version your app needs. A % in the command line, as in date +%F, must be written \%, or cron cuts the command there.

The command line has its own php.ini

PHP-FPM and Apache's module load one configuration; the php command loads another. On Debian and Ubuntu they live in /etc/php/8.3/fpm and /etc/php/8.3/cli. Run php --ini to see which file the command line reads, and php -m to list its extensions. A missing extension or a different memory_limit is the usual reason a script works in the browser and fails from cron. On the command line max_execution_time defaults to 0, no limit.

Don't rely on the working directory

Cron starts each job in the user's home directory, not the script's folder, so require 'config.php' looks for ~/config.php. Build paths from __DIR__, the directory of the file itself, or put cd /var/www/app && before the command.

$_SERVER['HTTP_HOST'], $_GET and sessions don't exist on the command line. Pass options as arguments instead (they arrive in $argv), and check PHP_SAPI so a cron script can't be run from a browser:

cron/send-emails.php
<?php
// Refuse to run from a browser
if (PHP_SAPI !== 'cli') {
    http_response_code(403);
    exit;
}

// Paths relative to this file, not to cron's working directory
require __DIR__ . '/../vendor/autoload.php';
$config = require __DIR__ . '/../config.php';

echo date('c'), " sending queued emails\n";

Log the output

Whatever the script prints, warnings and fatal errors included, goes to cron, which mails it to the crontab's owner, and on most servers that mail goes nowhere. Append >> /var/log/app/job.log 2>&1 to keep both output and errors in a file. To confirm cron started the job at all, read its log: journalctl -u cron on Debian and Ubuntu, journalctl -u crond on Fedora and RHEL.

Stop runs from overlapping

Cron starts the next run on time even if the last one is still going. A job every 5 minutes that once takes 7 ends up running twice at the same time, sending emails twice or locking the same rows. Put /usr/bin/flock -n /tmp/job.lock in front of the command: it holds a lock while the script runs, and -n makes the next run exit at once if the lock is taken. Inside PHP, flock() on a file you keep open does the same.

Laravel, Symfony and WordPress

Laravel keeps its schedule in code and needs one cron entry that runs schedule:run every minute; the framework decides which tasks are due:

crontab
* * * * * cd /var/www/app && /usr/bin/php artisan schedule:run >> /dev/null 2>&1

Symfony's Scheduler component runs as a long-lived worker (messenger:consume) rather than from cron, though many Symfony apps still call console commands from crontab the same way.

WordPress's built-in WP-Cron isn't cron: it runs due tasks when someone visits the site, so a quiet site runs them late. The next section shows how to turn it off and run the due events from real cron instead.

WordPress: replace wp-cron.php with a real cron job

wp-cron.php is the file that runs WordPress's scheduled events: publishing scheduled posts, update checks, plugin tasks such as backups and emails. Nothing calls it on a timer. On every page load WordPress checks whether an event is due, and if one is, it sends itself a request for /wp-cron.php?doing_wp_cron=… that it doesn't wait for. A site nobody visits runs nothing, and a lock keeps a second request from starting for 60 seconds (WP_CRON_LOCK_TIMEOUT).

To run it from cron instead, first add define( 'DISABLE_WP_CRON', true ); to wp-config.php, above the “stop editing” line. That only stops the request on page loads: wp-cron.php doesn't check the constant, so it still runs events whenever something calls it. Then pick one of three ways to call it, every 5 minutes (the shortest built-in WordPress recurrence is hourly, so that's plenty):

CommandRuns inNotes
wp cron event run --due-nowWP-CLI, command-line phpPrints each event it ran. Refuses to run as root unless you add --allow-root; on multisite add --url for each site
php wp-cron.phpCommand-line php, no WP-CLI neededRun it from the WordPress folder. No host name is set, so it doesn't suit multisite
curl https://example.com/wp-cron.phpThe web serverGets the web php.ini and time limits; fails if a security rule blocks wp-cron.php
crontab
# crontab of the user that owns the WordPress files (sudo crontab -u www-data -e)
# WP-CLI: runs every due event, then exits
*/5 * * * * /usr/local/bin/wp --path=/var/www/html cron event run --due-now >> /var/log/wp-cron.log 2>&1

# Without WP-CLI: run wp-cron.php with the command-line php
# */5 * * * * cd /var/www/html && /usr/bin/php wp-cron.php >> /var/log/wp-cron.log 2>&1

# No shell access to php at all: request it through the web server
# */5 * * * * /usr/bin/curl -fsS https://example.com/wp-cron.php > /dev/null

To check it, run wp cron event list: each hook with its next run. Events whose next run is in the past are overdue, which means nothing is calling wp-cron.php. Don't be alarmed when wp cron test fails with “The DISABLE_WP_CRON constant is set to true. WP-Cron spawning is disabled.” It tests the page-load request you just turned off, not your cron job.

The curl method fails with a 403, or “client denied by server configuration” in Apache's error log, when a security plugin or .htaccess rule blocks wp-cron.php; the WP-CLI and php methods don't go through the web server, so they aren't affected. ALTERNATE_WP_CRON is something else: it makes WordPress run events by redirecting a visitor's request, for hosts where the request to itself fails. Leave it off when real cron runs the events.

Run it through a URL instead

If you can't run php from cron at all, cron (or an outside cron service) can request a page with curl. The script then runs in the web server with the web php.ini and its time limits, and anyone who finds the URL can run it, so require a secret:

crontab
*/15 * * * * /usr/bin/curl -fsS "https://example.com/cron.php?key=LONG_RANDOM_SECRET" > /dev/null

Common schedules for PHP jobs

ExpressionRunsTypical job
* * * * *Every minute.Laravel and Symfony schedulers, queue checks
*/5 * * * *Every 5 minutes.Sending queued email, WordPress events
*/15 * * * *Every 15 minutes.Syncing with an API, clearing a cache
0 * * * *Every hour, on the hour.Imports, sitemaps, cleanup
30 2 * * *At 02:30 every day.Reports and backups, when traffic is low
0 6 * * 1At 06:00 on Mondays.A weekly summary email

Times are the server's

Cron reads the schedule in the server's time zone, which on most cloud servers is UTC. PHP's date.timezone setting changes what date() prints, not when cron starts the job.

Need the five fields themselves? See the cron expression syntax guide, or paste an expression into the cron explainer.