jbc/php-cron

Native PHP cron scheduler

Maintainers

Package info

github.com/joaomboni/Php-Cron

pkg:composer/jbc/php-cron

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-04-17 22:31 UTC

This package is auto-updated.

Last update: 2026-04-17 22:45:39 UTC


README

License: MIT

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+
  • pcntl extension

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