What is a PHP Task Scheduler?
A Job Scheduler is a library for PHP that allows you to run periodic tasks (cron jobs) directly from your application code, without relying on the system crond. It is especially useful on shared hosting, where access to crontab is limited, or in containerized environments (Docker).
Why do you need a scheduler?
- Autonomy — all periodicity rules are stored in PHP code, not in system configs.
- Flexibility — you can dynamically change the schedule, pause tasks, and log execution.
- Integration — easily integrates with frameworks (Laravel, Symfony) and ORM.
Installation
Install via Composer:
composer require dragonmantank/cron-expressionThis is a basic library for working with cron expressions. For a full-featured scheduler, peppeocchi/php-cron-scheduler is often used:
composer require peppeocchi/php-cron-schedulerMain Features
- Parsing cron expressions — checking if the current time matches the pattern (
* * * * *). - Multitasking — running multiple jobs in a single process.
- Parallelism — via
exec()orpcntl_fork(). - Logging — outputting stdout/stderr to a file.
- Error handling — retries, notifications.
Code Example
<?phprequire 'vendor/autoload.php';
use GO\\Scheduler;
$scheduler = new Scheduler();
// Task: every minute$scheduler->call(function() { echo "Running at " . date('Y-m-d H:i:s') . "";})->everyMinute();
// Task: every hour at 30 minutes$scheduler->call('cleanup_logs')->at('30 * * * *');
// Task: on weekdays at 9:00$scheduler->exec('php artisan report:generate')->at('0 9 * * 1-5');
// Run all due tasks$scheduler->run();When to Use
A PHP Task Scheduler is ideal for:
- sending scheduled email newsletters;
- cleaning up outdated data (logs, sessions);
- generating reports during off-peak hours;
- synchronizing with external APIs (once per hour);
- updating cache (every 5 minutes).
Do not replace the system cron for critical system operations (database backup, certificate renewal) — use it as an additional layer for business logic.