Master Dynamic PHP Cron Scheduling with the PHP‑Cron‑Scheduler Library
This guide explains how to replace static Linux cron jobs with the framework‑agnostic PHP‑Cron‑Scheduler, covering installation, configuration, job definition, timing expressions, best‑practice code examples, and log output to achieve flexible, secure, and maintainable task automation in PHP projects.
Overview
In software development, scheduled tasks are common, but traditional Linux cron lacks flexibility for dynamic timing adjustments, especially as projects grow and manage many cron entries across environments.
Cron Scheduler
PHP Cron Scheduler is a framework‑agnostic job scheduler that runs via a single scheduler.php entry point.
Installation
composer require peppeocchi/php-cron-schedulerOpen‑source repository:
https://github.com/peppeocchi/php-cron-schedulerHow it works
Create a scheduler.php file in the project root:
<?php
require_once __DIR__.'/vendor/autoload.php';
use GO\Scheduler;
$scheduler = new Scheduler();
// ... configure scheduled jobs ...
$scheduler->run();
?>Add a crontab entry to run the scheduler each minute:
* * * * * /usr/local/php-8.4/bin/php /home/tinywan/demo/scheduler.php 1>> /dev/null 2>&1Scheduling Jobs
By default all jobs run in the background; functions run in the foreground. Use inForeground() to force a command to run in the foreground, which is required for jobs that need to send output via email.
Execute a PHP script
$scheduler->php('path/to/my/script.php');Set execution time
Several helper methods let you define when a job should run. If none are called, the job runs every minute ( *). at() – accepts any expression supported by dragonmantank/cron-expression.
$scheduler->php('script.php')->at('* * * * *'); everyMinute()– runs each minute; optional interval argument specifies the minute step.
$scheduler->php('script.php')->everyMinute();
$scheduler->php('script.php')->everyMinute(5); hourly()– runs once per hour at minute 0 by default; optional minute argument customises the minute.
$scheduler->php('script.php')->hourly();
$scheduler->php('script.php')->hourly(53); daily()– runs once per day; optional hour and minute arguments or a "hour:minute" string give finer control.
$scheduler->php('script.php')->daily();
$scheduler->php('script.php')->daily(22, 03);
$scheduler->php('script.php')->daily('22:03');Best Practices
Example scheduler.php with strict types, timezone, binary path, and sample jobs:
<?php
declare(strict_types=1);
namespace think;
date_default_timezone_set('PRC');
require __DIR__.'/vendor/autoload.php';
$bin = '/usr/local/php-8.4/bin/php';
$script = __DIR__.'/think';
$scheduler = new \GO\Scheduler();
// Example raw Linux command
$scheduler->raw('ps aux | grep nginx');
// Example PHP job executed every 5 minutes
$scheduler->php($script.' order cancel', $bin)->everyMinute(5);
$scheduler->run();
?>Sample log output shows timestamps for job start and end, confirming correct execution.
Conclusion
The article demonstrates how to implement a flexible, secure, and easy‑to‑manage dynamic cron system in PHP, greatly improving project scalability and operational efficiency.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
