PHP cron jobs: run a script on a schedule
On this page
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>&1That 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, or read the cron syntax cheat sheet.
Why does a PHP script behave differently under cron?
- It is the CLI, not the web server. The command-line binary has no request:
$_SERVERentries such asREQUEST_URIare missing, and$argvholds the command-line arguments. Test withPHP_SAPI === 'cli'before you rely on either. - The time limit is gone. On the command line
max_execution_timeis 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 --inishows which one, andphp -d memory_limit=512M script.phpoverrides 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 covers the shell side, and cron job not running 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>&1The framework decides which of its tasks are due, so the crontab holds just that one line. See the Laravel scheduler guide 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 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-2d4f8a7e3c10To report from inside the script, with a failure signal when it throws, use the PHP snippet. Create a check with the same schedule and CronMissed alerts you when a ping is late or the job reports a failure.
Sources
- crontab(5) manual page , read 26 November 2026
- PHP manual: differences of the CLI SAPI , read 26 November 2026
Questions and answers
- Use * * * * * as the schedule, followed by the PHP binary and the script. Add flock -n if a run can take longer than a minute.
- 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.
- 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.
- 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.
How do I run a PHP script every minute with cron?
Can cron call a PHP script by its URL?
Where do I find the PHP binary for cron?
Do I need a library to schedule tasks in PHP?
Create a check, add one curl to the job, and CronMissed alerts you when a ping is late or reports a failure.
Start monitoring