Home › Cron Jobs with PHP
Cron Jobs with PHP
How to run PHP scripts automatically on a schedule. Covers Linux crontab, cPanel, common mistakes and framework options.
The Basic Cron Entry for PHP
A cron job that runs a PHP script needs two things: the path to the PHP binary and the path to the script. Both must be absolute paths. This is the single most common mistake when setting up PHP cron jobs. PHP CLI documentation: php.net - Using PHP from the command line.
30 3 * * * /usr/bin/php /var/www/html/my-script.php
Runs my-script.php every day at 3:30 AM.
Find your PHP binary path first
Do not assume /usr/bin/php. On many servers, especially shared hosting, PHP lives somewhere else.
Run this in your terminal:
which php
If you have multiple PHP versions, you may get /usr/bin/php8.2 or /opt/plesk/php/8.2/bin/php.
Use the exact path that matches the PHP version your project needs.
Step by Step Setup
1. Open the crontab
crontab -e
This opens the crontab for the current user. If you need to run the script as a different user, use sudo crontab -u username -e.
2. Add the cron entry
30 3 * * * /usr/bin/php /var/www/html/script.php >> /var/log/myjob.log 2>&1
Always redirect output to a log file. Without this, you will never know if the job failed.
3. Verify it was saved
crontab -l
Lists all scheduled jobs for the current user. If your entry appears here, cron will pick it up automatically.
4. Test the command manually first
/usr/bin/php /var/www/html/script.php
Run the exact command you put in the crontab before waiting for cron to execute it. If it fails here, it will fail in cron too.
Common PHP Cron Patterns
| Schedule | Expression | Full cron entry |
|---|---|---|
| Daily backup at 2 AM | 0 2 * * * |
0 2 * * * /usr/bin/php /var/www/backup.php |
| Every 5 minutes | */5 * * * * |
*/5 * * * * /usr/bin/php /var/www/queue.php |
| Weekly report (Monday 8 AM) | 0 8 * * 1 |
0 8 * * 1 /usr/bin/php /var/www/report.php |
| First of month | 0 0 1 * * |
0 0 1 * * /usr/bin/php /var/www/monthly.php |
Things That Go Wrong with PHP Cron Jobs
Script works manually but not in cron
Almost always a PATH problem. Your shell has ~/.bashrc loaded with all your environment variables.
Cron does not. Database credentials, API keys set as env vars, composer autoload paths -
none of them are available unless you set them explicitly in the script or at the top of the cron entry.
Relative paths inside the script
When cron runs your script, the working directory is not /var/www/html.
It is usually the home directory of the cron user.
Any include, require or file_get_contents with a relative path will fail.
Use __DIR__ or dirname(__FILE__) to build absolute paths inside your PHP scripts.
Memory or time limit too low
PHP CLI uses its own php.ini, not the web server one.
The memory limit and max execution time may be different - usually more generous, but worth checking.
Run php --ini to find which ini file the CLI is using.
Overlapping runs
Cron does not check if the previous run is still going. If your job takes 10 minutes and runs every 5, you will have two instances running simultaneously. Use a lock file or a database flag to prevent overlaps: check at the start, set on start, clear on finish.
PHP Cron Job Libraries
When the crontab approach becomes hard to manage, these libraries let you define schedules in PHP code itself.
peppeocchi/php-cron-scheduler
Framework-agnostic. Define all your jobs in one PHP file, add a single crontab entry that calls it every minute.
* * * * * /usr/bin/php scheduler.php
Crunz
Similar approach, more expressive API. Good if you want readable schedule definitions close to your application code.
* * * * * vendor/bin/crunz schedule:run
Laravel Scheduler
If you are using Laravel, use its built-in Task Scheduling instead. One crontab entry drives everything.
* * * * * php artisan schedule:run
Generate the cron expression for your PHP job
Use the generator to build the schedule, then paste it in front of your PHP command.