failover.io
  1. Home
  2. Guides
  3. PHP cron job

Run a PHP script with a cron job

One crontab line runs your script on schedule. This guide covers the paths, the user, the logs and what happens when it fails.

Updated 19 November 20267 min read

To run a PHP script on a schedule, add a line to the crontab of the user that should run it. This runs report.php every 5 minutes:

*/5 * * * * /usr/bin/php /var/www/app/report.php >> /var/log/app/report.log 2>&1

The rest of this guide covers the details that make that line work every time: the PHP path, the working directory, which user runs it, overlapping runs, frameworks and how to find out when it stops.

Add the cron job

  1. Find the PHP binary with which php. Cron runs with a minimal PATH (often just /usr/bin:/bin), so always write the full path, like /usr/bin/php or /usr/bin/php8.3.
  2. Open the crontab with crontab -e. To run as the web server user, so files the script writes have the right owner, use sudo crontab -u www-data -e.
  3. Add the line with a schedule, the PHP path and the full path to the script, then save. Cron reads the change on its own; no restart needed.
  4. Check it's there with crontab -l (or sudo crontab -u www-data -l).

Need a different schedule? */5 * * * * is every 5 minutes; 0 * * * * is every hour; all common schedules are on one page.

Paths and the working directory

Cron starts your script from the user's home directory, not from the script's folder. A relative require 'config.php'; that works in your terminal fails under cron. Two fixes:

// in the script: paths relative to the script's own folder
require __DIR__ . '/config.php';
# or in the crontab: change directory first
*/5 * * * * cd /var/www/app && /usr/bin/php report.php

Cron also doesn't load your shell profile, so environment variables you set in .bashrc aren't there. Put them in the script, in a .env file the script reads, or at the top of the crontab.

The CLI uses its own php.ini

PHP on the command line often reads a different php.ini than PHP-FPM or Apache, with different extensions, memory limit and time zone. Run php --ini to see which file the CLI loads. If the script works in the browser but not from cron, compare php -m with the extensions your web server has.

See what happened

Without a redirect, cron mails the output to the user, and on most servers that mail goes nowhere. Send it to a log file instead:

*/5 * * * * /usr/bin/php /var/www/app/report.php >> /var/log/app/report.log 2>&1

>> appends output, 2>&1 adds errors to the same file. To confirm cron started the job at all, check the system log: grep CRON /var/log/syslog on Debian and Ubuntu, or journalctl -u cron.

Stop runs from overlapping

If one run can take longer than the interval, the next starts while the first is still working, and both touch the same data. flock skips a run while the previous one holds the lock:

*/5 * * * * flock -n /tmp/report.lock /usr/bin/php /var/www/app/report.php

Laravel, Symfony and WordPress

Laravel needs one cron line that runs its scheduler every minute; the schedule itself lives in your code:

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

Symfony console commands run like any script: */5 * * * * cd /var/www/app && /usr/bin/php bin/console app:send-reports.

WordPress runs its own "cron" only when someone visits the site, so on a quiet site scheduled posts and tasks run late. Turn that off in wp-config.php with define('DISABLE_WP_CRON', true); and run it from the real cron instead, with WP-CLI:

*/5 * * * * cd /var/www/html && wp cron event run --due-now

No shell access? Call the script over HTTP

On shared hosting without a terminal, the hosting panel's cron feature can call a URL instead. Protect it with a secret so strangers can't trigger it:

*/5 * * * * curl -fsS "https://example.com/cron/report.php?key=LONG_RANDOM_SECRET" > /dev/null

Keep in mind that the request then runs under the web server's time limits, which can stop a long job half way.

Exit codes: make failures visible

Cron only knows whether your script exited with 0. An uncaught exception or a fatal error exits with 255; a script that catches its errors and carries on exits with 0 even when nothing worked. End failed runs explicitly:

if (!$ok) {
    fwrite(STDERR, "report failed: $reason\n");
    exit(1);
}

Get an alert when the job stops

A PHP cron job can stop without a sound: a server rebuilt without its crontab, a PHP upgrade that removed an extension, a full disk. Add a heartbeat ping that only runs when the script exits with 0:

*/5 * * * * /usr/bin/php /var/www/app/report.php && curl -fsS -m 10 -X POST https://api.failover.io/heartbeat/<your-monitor-id>

Create a heartbeat monitor on failover.io with an expected interval of 300 seconds and a couple of minutes of grace. If a run fails or never starts, the ping doesn't arrive and you get an alert by email, Slack or Telegram, and by SMS or phone call on paid plans. More in how to monitor cron jobs.

Frequently asked questions

How do I run a PHP script with cron?

Run crontab -e and add a line with the schedule, the full path to PHP and the full path to the script, for example */5 * * * * /usr/bin/php /var/www/app/report.php.

Why does my PHP script work in the terminal but not in cron?

Usually a relative path (cron starts in the home directory), a missing environment variable, a different php.ini for the CLI, or PHP not being on cron's PATH. Use full paths and __DIR__, and log the output to a file to see the error.

Where is the PHP path for cron?

Run which php in a terminal. It is usually /usr/bin/php, or a versioned binary like /usr/bin/php8.3.

How do I run the Laravel scheduler with cron?

Add one line that runs every minute: * * * * * cd /path/to/app && php artisan schedule:run >> /dev/null 2>&1. Laravel decides which tasks are due.

How do I know if my PHP cron job failed?

Make the script exit with a non-zero code on failure, then add a heartbeat ping after it with &&. A monitor that expects the ping alerts you when it doesn't arrive.