Maintenance

Schedule and Run Cron Jobs Reliably in PHP

Build scheduled PHP tasks that avoid overlap, handle time zones, report failures and recover from interrupted execution.

Make scheduled work explicit

Cron is suitable for periodic cleanup, synchronization and report generation. Use the intended PHP CLI binary explicitly, because a server may have multiple PHP versions and different CLI configuration from the web runtime.

Prevent overlapping runs

If a task must not overlap with itself, use an atomic lock such as a database lock, filesystem lock or scheduler-level overlap prevention. Define stale-lock recovery so a crashed process does not block future runs indefinitely.

Design for retries

A cron task can stop halfway through. Process records in bounded batches and record progress. Make each unit of work idempotent so rerunning the task does not duplicate changes or notifications.

Handle time zones and daylight saving

Store timestamps in UTC where practical. Schedule jobs according to a clearly documented time zone. A local-time schedule can run twice or not at all around daylight-saving transitions in some regions; critical schedules should account for this explicitly.

Capture outcomes

Log start, finish, duration, processed count and failure details. Send alerts for failed or overdue jobs, but avoid sending an email on every retry if that could create a notification storm.

Checklist

  • Use the correct PHP CLI binary.
  • Prevent unintended overlapping executions.
  • Make tasks resumable and idempotent.
  • Process bounded batches.
  • Define time-zone semantics.
  • Log duration, counts and errors.
  • Alert on failures and missed schedules.

References

PHP CLI · Cron manual

Share this page Help other developers discover this resource.