This article provides an extended look at how to create PHP cron jobs in Linux.
Creating PHP Cron Jobs on a Linux Server
Cron is the standard Linux scheduler used to run scripts automatically at defined intervals. PHP cron jobs are commonly used for automation tasks such as backups, billing runs, log cleanup, monitoring scripts, email campaigns, and application maintenance.
This guide explains how to create, configure, and customize PHP cron jobs using command-line flags and parameters.
-
Understanding Cron
Cron is a background daemon that executes commands based on a defined schedule stored in a crontab file.
A cron schedule has five time fields followed by the command to execute.
* * * * * command_to_run │ │ │ │ │ │ │ │ │ └── Day of week (0–7) (Sun = 0 or 7) │ │ │ └──── Month (1–12) │ │ └────── Day of month (1–31) │ └──────── Hour (0–23) └────────── Minute (0–59)Example:
0 2 * * * /usr/bin/php /scripts/backup.phpThis runs
backup.phpevery day at 2:00 AM. -
Verify PHP CLI is Installed
Cron jobs should use the PHP CLI binary, not the web version.
Check the PHP CLI path:
which phpTypical results:
/usr/bin/phpCheck version:
php -vExample output:
PHP 8.2.10 (cli) -
Create a Basic PHP Cron Script
Example script:
nano /scripts/test-cron.phpExample code:
<?php $date = date("Y-m-d H:i:s"); file_put_contents( "/tmp/cron_test.log", "Cron executed at $date\n", FILE_APPEND ); ?>Test it manually:
php /scripts/test-cron.phpCheck output:
cat /tmp/cron_test.log -
Editing the Crontab
Open the crontab editor:
crontab -eAdd a job:
*/5 * * * * /usr/bin/php /scripts/test-cron.phpThis runs every 5 minutes.
Save and exit.
Verify cron jobs:
crontab -lTip! Customize the crontab editor:EDITOR=nano crontab -e -
Using Full Paths (Very Important)
Cron runs with a minimal environment, so always use absolute paths.
Incorrect:
php test-cron.phpCorrect:
/usr/bin/php /scripts/test-cron.php -
Redirecting Output and Errors
Cron jobs generate output which may be emailed or logged.
Redirect output:
*/5 * * * * /usr/bin/php /scripts/test.php >> /var/log/php-cron.logRedirect errors:
*/5 * * * * /usr/bin/php /scripts/test.php >> /var/log/php-cron.log 2>&1Explanation:
>> append output 2>&1 send errors to same log -
PHP CLI Flags for Cron Jobs
PHP CLI supports flags that customize execution.
Common flags include:
Flag Purpose -cSpecify custom php.ini -dOverride configuration values -qSuppress HTTP headers -fExecute PHP script file -rRun PHP code directly -mShow loaded modules -nIgnore php.ini -
Using the
-cFlag (Custom php.ini)The
-coption specifies a custom configuration file.Example:
/usr/bin/php -c /opt/custom-php.ini /scripts/worker.phpUse case:
Different memory limits for cron tasks.
Example custom config:
memory_limit = 1024M max_execution_time = 0Cron entry:
*/10 * * * * /usr/bin/php -c /opt/php-cron.ini /scripts/heavy-task.php -
Using the
-dFlag (Override Settings)The
-dflag temporarily overrides PHP settings.Example:
php -d memory_limit=512M script.phpCron example:
0 * * * * /usr/bin/php -d memory_limit=1024M /scripts/report.phpMultiple overrides:
php -d memory_limit=512M -d max_execution_time=0 script.phpCron example:
0 1 * * * /usr/bin/php -d memory_limit=1G -d max_execution_time=0 /scripts/import.php -
Using the
-qFlagThe
-qoption suppresses HTTP headers.Useful for older PHP scripts designed for CGI.
Example:
php -q script.phpCron example:
*/15 * * * * /usr/bin/php -q /scripts/legacy-script.php -
Using the
-fFlagThe
-fflag explicitly specifies a script file.Example:
php -f /scripts/job.phpCron example:
*/10 * * * * /usr/bin/php -f /scripts/cache-cleaner.php -
Passing Arguments to PHP Scripts
PHP scripts can accept parameters.
Example script:
nano /scripts/backup.phpExample code:
<?php $type = $argv[1] ?? "full"; echo "Running $type backup\n"; ?>Run manually:
php backup.php fullCron example:
0 3 * * * /usr/bin/php /scripts/backup.php full -
Combining Flags and Arguments
Example advanced cron job:
*/30 * * * * /usr/bin/php -d memory_limit=512M -d max_execution_time=0 /scripts/process-queue.php --limit=100Explanation:
-d memory_limit=512M -d max_execution_time=0 --limit=100 passed to script -
Running PHP Inline with
-rYou can execute PHP code directly.
Example:
php -r 'echo date("Y-m-d H:i:s");'Cron example:
*/10 * * * * /usr/bin/php -r 'file_put_contents("/tmp/time.log", date("c").PHP_EOL, FILE_APPEND);' -
Running as a Specific User
Cron jobs should run under the correct user.
Edit the user's crontab:
crontab -eSystem-wide cron:
/etc/crontabExample:
0 1 * * * www-data /usr/bin/php /var/www/scripts/cleanup.php -
Locking Cron Jobs (Prevent Overlap)
Prevent jobs from running simultaneously.
Example using
flock:*/5 * * * * flock -n /tmp/job.lock /usr/bin/php /scripts/job.php -
Monitoring Cron Jobs
Check cron logs:
grep CRON /var/log/syslogOn RHEL / AlmaLinux:
grep CRON /var/log/cron -
Example Production Cron Jobs
-
Queue Worker
* * * * * /usr/bin/php /var/www/app/artisan queue:work -
Database Cleanup
0 4 * * * /usr/bin/php -d memory_limit=512M /scripts/db-clean.php -
Cache Purge
*/30 * * * * /usr/bin/php /scripts/cache-purge.php >> /var/log/cache.log 2>&1 -
Backup Job
0 3 * * * /usr/bin/php -d memory_limit=2G /scripts/backup.php full >> /var/log/backup.log 2>&1
-
-
Security Best Practices
-
Use CLI PHP
Always use:
/usr/bin/phpnot:
/usr/local/bin/php-cgi -
Restrict Permissions
chmod 700 /scripts -
Avoid Running as Root
Use a dedicated user:
www-data deploy automation -
Log Everything
>> /var/log/script.log 2>&1
-
-
Example Enterprise PHP Cron Stack
Example automation environment:
/opt/cron/ ├── workers │ ├── queue.php │ └── billing.php ├── logs │ └── cron.log └── locksExample cron entries:
*/1 * * * * flock -n /opt/cron/locks/queue.lock /usr/bin/php /opt/cron/workers/queue.php >> /opt/cron/logs/cron.log 2>&1 0 * * * * /usr/bin/php -d memory_limit=512M /opt/cron/workers/billing.php >> /opt/cron/logs/cron.log 2>&1 -
Testing Cron Jobs Safely
Test with:
run-parts --test /etc/cron.hourlyOr run manually:
sudo -u www-data /usr/bin/php /scripts/test.php -
Troubleshooting
-
Cron not running
Check service:
systemctl status crondStart if needed:
systemctl enable crond systemctl start crond -
PATH problems
Add PATH in crontab:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin -
Permission errors
Check user permissions:
ls -l script.php
-
-
Example WHMCS Cron Job
Since many hosting platforms (like WHMCS) rely heavily on PHP cron automation:
*/5 * * * * /opt/cpanel/ea-php84/root/usr/bin/php -c /opt/cpanel/ea-php84/root/etc/php.ini -d date.timezone='America/Chicago' -q /path/to/your/cron.php >> /path/to/your/logfile.log 2>&1The example calls the specific PHP version binary (
/opt/cpanel/ea-php84/root/usr/bin/php), specifies custom php.ini (-c /opt/cpanel/ea-php84/root/etc/php.ini), declares custom timezone (-d date.timezone='America/Chicago'), suppressing HTTP headers (-q), and logs the results to a log file (>> /path/to/your/logfile.log 2>&1).
Conclusion
You now know how to create PHP cron jobs in Linux. Feel free to bookmark this page for reference.

👀 Choose SSD-powered VPS servers for increased speed, power, and security! Now 50% off- starting from only $3.19/mo.