---
title: "PHP cron jobs: run a PHP script on a schedule"
url: "https://cronmissed.com/learn/php-cron-jobs.md"
canonical_url: "https://cronmissed.com/learn/php-cron-jobs"
last_updated: "2026-11-26"
type: "conceptual"
summary: "Run a PHP script from cron with the CLI binary, absolute paths, the right user and a log file, avoid overlapping runs, and get an alert when it stops."
related: ["https://cronmissed.com/learn.md", "https://cronmissed.com/guides/laravel-scheduler.md", "https://cronmissed.com/snippets/php.md", "https://cronmissed.com/learn/overlapping-cron-runs.md", "https://cronmissed.com/learn/cron-environment-and-path.md", "https://cronmissed.com/learn/create-a-cron-job.md", "https://cronmissed.com/learn/python-cron-jobs.md"]
---

# PHP cron jobs: run a PHP script on a schedule

> **For AI agents:** the complete index of CronMissed pages is [llms.txt](https://cronmissed.com/llms.txt). Every page is also served as markdown at its URL plus .md.

Run a PHP script from cron with the CLI binary, absolute paths, the right user and a log file, avoid overlapping runs, and get an alert when it stops.

To run a PHP cron job, add a crontab line that names the PHP command-line binary and the script by absolute path. Cron starts with a bare environment and a different working directory from your shell, so a script that works when you test it by hand can fail there until the paths and the user are spelled out.

## How do you schedule a PHP script with cron?

Find the binary with `command -v php`, then add one line with `crontab -e`: five schedule fields, the binary, the script and a redirect for the output.

```
*/10 * * * * /usr/bin/php /var/www/app/bin/cleanup.php >> /var/log/app-cleanup.log 2>&1
```

That runs `cleanup.php` every 10 minutes and appends everything it prints to a log. The script does not need to be executable when you name the interpreter. If the schedule needs a second look, paste it into the [cron expression translator](https://cronmissed.com/tools/cron-explainer.md), or read the [cron syntax cheat sheet](https://cronmissed.com/learn/cron-syntax-cheat-sheet.md).

## Why does a PHP script behave differently under cron?

- **It is the CLI, not the web server.** The command-line binary has no request: `$_SERVER` entries such as `REQUEST_URI` are missing, and `$argv` holds the command-line arguments. Test with `PHP_SAPI === 'cli'` before you rely on either.
- **The time limit is gone.** On the command line `max_execution_time` is 0, so a script that hangs will run until something stops it. Set your own limit or a timeout in the command.
- **The working directory is not the script's folder.** The CLI does not change into the directory of the script. Open files by absolute path, or `cd /var/www/app && /usr/bin/php bin/cleanup.php`.
- **A different php.ini.** The CLI often reads a different configuration file than the web server does. `php --ini` shows which one, and `php -d memory_limit=512M script.php` overrides one setting for a run.
- **A different user.** A job in your crontab runs as you, not as the web server user, so it may not be able to write to the files the site writes. Use that user's crontab: `sudo crontab -u www-data -e`.

[Cron environment and PATH](https://cronmissed.com/learn/cron-environment-and-path.md) covers the shell side, and [cron job not running](https://cronmissed.com/learn/cron-job-not-running.md) is the full checklist.

## How do you run the Laravel scheduler or a framework command?

Frameworks keep their own schedule in code and ask for one cron line that runs every minute. Laravel's line changes into the project folder and runs the scheduler:

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

The framework decides which of its tasks are due, so the crontab holds just that one line. See the [Laravel scheduler guide](https://cronmissed.com/guides/laravel-scheduler.md) for monitoring each task.

## How do you stop two runs from overlapping?

If a run can outlast its interval, wrap the command in `flock` so that a second copy exits at once instead of running beside the first:

```
*/10 * * * * flock -n /tmp/cleanup.lock /usr/bin/php /var/www/app/bin/cleanup.php >> /var/log/app-cleanup.log 2>&1
```

`-n` makes `flock` give up instead of waiting for the lock. [How to stop overlapping cron job runs](https://cronmissed.com/learn/overlapping-cron-runs.md) shows the other options.

## How do you know the PHP job ran?

Have the job report to a check when the work is done. Joined with `&&`, the request is sent only if the script exits with status 0, and its timeout means a monitoring problem can never fail the job:

```
*/10 * * * * /usr/bin/php /var/www/app/bin/cleanup.php && curl -fsS --max-time 10 --retry 5 -o /dev/null https://ping.cronmissed.com/5f0c1b2e-7a3d-4c8e-9b61-2d4f8a7e3c10
```

To report from inside the script, with a failure signal when it throws, use the [PHP snippet](https://cronmissed.com/snippets/php.md). Create a check with the same schedule and CronMissed alerts you when a ping is late or the job reports a failure.

## Questions

### How do I run a PHP script every minute with cron?

Use `* * * * *` as the schedule, followed by the PHP binary and the script. Add `flock -n` if a run can take longer than a minute.

### Can cron call a PHP script by its URL?

Yes, with `curl`. It runs the script under the web server's settings and time limit, which helps when the script needs the web configuration. The cost is a web request per run, so the command-line binary is the usual choice.

### Where do I find the PHP binary for cron?

Run `command -v php` in a shell. Shared hosting panels show the path in their cron job page, and it can include a version, such as a folder named for PHP 8.

### Do I need a library to schedule tasks in PHP?

No. Cron alone is enough. Libraries and frameworks let you write the schedule in PHP, but they still rely on one crontab line that runs every minute.

## Sources

- [crontab(5) manual page](https://man7.org/linux/man-pages/man5/crontab.5.html), read 26 November 2026
- [PHP manual: differences of the CLI SAPI](https://www.php.net/manual/en/features.commandline.differences.php), read 26 November 2026

## Related pages

- [Laravel scheduler](https://cronmissed.com/guides/laravel-scheduler.md)
- [PHP](https://cronmissed.com/snippets/php.md)
- [Overlapping cron runs](https://cronmissed.com/learn/overlapping-cron-runs.md)
- [Cron environment and PATH](https://cronmissed.com/learn/cron-environment-and-path.md)
- [Create a cron job](https://cronmissed.com/learn/create-a-cron-job.md)
- [Python cron jobs](https://cronmissed.com/learn/python-cron-jobs.md)

## Related

- [Cron monitoring explained: concepts and fixes](https://cronmissed.com/learn.md)
- [Monitor the Laravel task scheduler](https://cronmissed.com/guides/laravel-scheduler.md)
- [Ping CronMissed from PHP](https://cronmissed.com/snippets/php.md)
- [How to stop overlapping cron job runs](https://cronmissed.com/learn/overlapping-cron-runs.md)
- [Cron environment variables and PATH explained](https://cronmissed.com/learn/cron-environment-and-path.md)
- [How to create a cron job on Linux and Ubuntu](https://cronmissed.com/learn/create-a-cron-job.md)
- [Python cron jobs: schedule a script with cron](https://cronmissed.com/learn/python-cron-jobs.md)
