Advertisement
Intermediate PHP, PHP CLI Scripting

How do you schedule a PHP script to run automatically at regular intervals using cron on a Linux server?

Short answer

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…

Save PHP mock interview
Advertisement
AModel answer
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.
Example

# Crontab entry running a script every night at midnight 0 0 * * * /usr/bin/php /var/www/scripts/cleanup.php

Real-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.
Likely follow-ups
  • What does each field in a standard crontab schedule entry represent?
  • How do modern frameworks like Laravel provide their own built in task scheduling on top of a single cron entry?
Related topics