toflar / cronjob-supervisor
A simple supervisor for processes that can be triggered using a minutely cronjob
Fund package maintenance!
Requires
- php: ^8.1
- symfony/filesystem: ^6.0 || ^7.0 || ^8.0
- symfony/lock: ^6.0 || ^7.0 || ^8.0
- symfony/process: ^6.0 || ^7.0 || ^8.0
Requires (Dev)
- phpunit/phpunit: ^10.2
- terminal42/contao-build-tools: @dev
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-10-23 08:18:08 UTC
README
Need to have a number of workers on some server but have no access to any daemon like supervisord or the likes but
can configure a minutely cronjob? This library might come in handy for you then.
- Installation
composer require toflar/cronjob-supervisor
- Create your
runner.php:
<?php require_once 'vendor/autoload.php'; use Symfony\Component\Process\Process; use Toflar\CronjobSupervisor\BasicCommand; use Toflar\CronjobSupervisor\Supervisor; $supervisor = Supervisor::withDefaultProviders('/some/directory/you/want/to/store/your/state'); $supervisor ->withCommand(new BasicCommand('sleep 10', 2, function () { return new Process(['sleep', '10']); })) ->withCommand(new BasicCommand('sleep 29', 4, function () { return new Process(['sleep', '29']); })) ->supervise() ;
- Configure the minutely cronjob
* * * * * /path/to/your/php/binary/php runner.php
That's it. The Supervisor will take care that even if your jobs are still running after a minute has passed, only
ever your maximum number of processes will be created.
For this to work, it uses multiple providers to check if processes are still running. Currently supported are:
ps -p <pid>[Linux]tasklist /FI PID eq <pid>[Windows]flock- [Linux, Windows]
Which means it runs everywhere (flock as the fallback).
Worker timeouts and output
The supervisor respects each Symfony Process timeout, including idle timeouts. Symfony Process defaults to a
60-second timeout. For workers intended to run indefinitely, explicitly create them with timeout: null:
return (new Process(['php', 'worker.php'], timeout: null))->disableOutput();
Disable output when it is not needed, or redirect worker logs to a managed destination. Captured output is retained by Symfony Process for the lifetime of the Process object and can spill to temporary files.
A timed-out worker is stopped without aborting supervision of other workers. Its slot is replaced on the next
supervision tick. Listen to Supervisor::EVENT_PROCESS_TIMED_OUT to receive the PID and the Symfony
ProcessTimedOutException. The regular EVENT_PROCESS_FINISHED event still follows when the worker is collected.