PHP › PHP CLI Scripting
How do you schedule a PHP script to run automatically at regular intervals using cron on a Linux server?
IntermediateAnswer
Cron is a standard Linux utility for scheduling recurring tasks, and you can configure it to run a PHP script at specific times or intervals, such as every night at midnight, by adding an entry to the system's crontab file specifying the schedule and the exact command to run, which is commonly used for tasks like sending scheduled reports, cleaning up old data, or triggering a queued job processor at regular intervals.
# Crontab entry running a script every night at midnight
0 0 * * * /usr/bin/php /var/www/scripts/cleanup.phpReal-world example A company schedules a PHP script to run automatically every night through cron, generating and emailing a daily sales summary report to management without requiring anyone to manually trigger it each day.