Simplify Task Automation with PHP Cron Scheduler: A Step‑by‑Step Guide
This guide explains how to replace manual crontab entries with the framework‑agnostic PHP Cron Scheduler, showing how to set up a single scheduler.php entry, define PHP scripts, raw commands, anonymous functions, and configure flexible execution times such as every minute, hourly or daily.
Introduction
Traditional cron management requires creating a separate crontab line for each scheduled task and editing it via SSH, which quickly becomes cumbersome. PHP Cron Scheduler is a framework‑agnostic library that lets you define all jobs in code and run them through a single scheduler.php entry.
GitHub repository: https://github.com/peppeocchi/php-cron-scheduler
How It Works
Create a scheduler.php file in your project root and add the following bootstrap code:
<?php
require_once __DIR__.'/vendor/autoload.php';
use GO\Scheduler;
$scheduler = new Scheduler();
// configure jobs here …
$scheduler->run();Then add a single crontab line that runs this file every minute:
* * * * * /path/to/php /path/to/scheduler.php >> /dev/null 2>&1The scheduler will evaluate all defined jobs each minute and execute those that are due.
Scheduling Jobs
By default, jobs run in the background; PHP scripts and raw commands are backgrounded, while 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.
Running a PHP Script
$scheduler->php('path/to/my/script.php');The php() method accepts four parameters: script path, PHP binary (optional), an array of arguments, and an identifier.
$scheduler->php(
'path/to/my/script.php', // script to execute
'/usr/local/bin/php', // optional PHP binary
[
'-c' => 'ignore',
'--merge' => null,
],
'myCustomIdentifier'
);Running a Raw Command
$scheduler->raw('ps aux | grep httpd');The raw() method also takes three parameters: the command string, an optional arguments array, and an identifier.
$scheduler->raw(
'mycommand | myOtherCommand',
[
'-v' => '6',
'--silent' => null,
],
'myCustomIdentifier'
);Scheduling an Anonymous Function
$scheduler->call(function () {
return true;
});The call() method accepts the closure, an optional arguments array, and an identifier.
$scheduler->call(
function ($args) {
return $args['user'];
},
[
['user' => $user],
],
'myCustomIdentifier'
);When passing key‑value pairs, wrap them in an inner array; the keys are ignored unless you explicitly handle them inside the closure.
Setting Execution Times
If no timing method is called, a job runs every minute (the * expression). The scheduler supports any dragonmantank/cron-expression pattern via at(), as well as convenient helpers:
Every Minute
$scheduler->php('script.php')->everyMinute();
$scheduler->php('script.php')->everyMinute(5); // every 5 minutesHourly
$scheduler->php('script.php')->hourly();
$scheduler->php('script.php')->hourly(53); // at minute 53 of each hourDaily
$scheduler->php('script.php')->daily();
$scheduler->php('script.php')->daily(22, 03); // 22:03 each day
$scheduler->php('script.php')->daily('22:03');Project Application Example
A real‑world usage in a ThinkPHP project:
<?php
declare(strict_types=1);
namespace think;
date_default_timezone_set('PRC');
require __DIR__.'/vendor/autoload.php';
$bin = '/usr/local/php-8.2/bin/php';
$script = __DIR__.'/think';
$scheduler = new \GO\Scheduler();
// Auto‑ship orders daily at 00:30
$scheduler->php($script.' order delivery', $bin)->daily(0, 30);
// Auto‑cancel orders every 5 minutes
$scheduler->php($script.' order cancel', $bin)->everyMinute(5);
$scheduler->run();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.
