jbc / php-cron
Native PHP cron scheduler
v1.0.0
2026-04-17 22:31 UTC
Requires
- php: >=8.1
- psr/log: ^3.0
- symfony/console: ^7.0
- symfony/event-dispatcher: ^7.0
- symfony/lock: ^7.0
- symfony/process: ^7.0
README
Native PHP cron scheduler — no crontab required.
A pure PHP task scheduler that does not depend on the system crontab. Perfect for PHP applications that need to run periodic tasks without additional server configuration.
Requirements
- PHP 8.1+
pcntlextension
Installation
composer require jbc/php-cron
Basic usage
use Jbc\PhpCron\Scheduler; $scheduler = new Scheduler( timezone: new \DateTimeZone('America/Sao_Paulo'), ); $scheduler->schedule('* * * * *', function () { echo 'Runs every minute' . PHP_EOL; }); $scheduler->schedule('0 9 * * *', function () { echo 'Runs every day at 9am' . PHP_EOL; }); $scheduler->run();
Supported expressions
| Expression | Description |
|---|---|
* * * * * |
Every minute |
*/5 * * * * |
Every 5 minutes |
0 9 * * * |
Every day at 9am |
0 9 * * 1-5 |
Weekdays at 9am |
0 0 1 * * |
First day of every month |
@hourly |
Every hour |
@daily |
Every day at midnight |
@weekly |
Every week |
@monthly |
Every month |
Job options
$scheduler ->schedule('* * * * *', fn() => myFunction()) ->name('my-job') ->withoutOverlapping() ->onlyIf(fn() => true) ->onError(fn(\Throwable $e) => logger($e));
| Method | Description |
|---|---|
->name('name') |
Sets a unique name for the job |
->withoutOverlapping() |
Prevents simultaneous execution of the same job |
->onlyIf(callable) |
Only runs if the condition returns true |
->onError(callable) |
Catches errors without stopping the scheduler |
Running
php bin/cron
Production with Supervisor
[program:php-cron] command=php /var/www/html/php-cron/bin/cron autostart=true autorestart=true stderr_logfile=/var/log/php-cron.err.log stdout_logfile=/var/log/php-cron.out.log
License
MIT © João Bonifacio