azurre / php-cron-scheduler
定时任务管理器
1.2
2021-07-06 15:45 UTC
This package is auto-updated.
Last update: 2024-09-06 22:35:18 UTC
README
简单的定时任务管理器。将您的项目定时任务保持在您的项目中!
安装
使用composer安装此包
composer require azurre/php-cron-scheduler
使用方法
将调度器启动器添加到cron
$ crontab -e
* * * * * /usr/bin/php /path/to/project/scheduler.php >> /path/to/project/scheduler.log 2>&1
scheduler.php 示例
$loader = require_once __DIR__ . '/vendor/autoload.php'; use Azurre\Component\Cron\Scheduler; use Azurre\Component\Cron\Expression; $e = new Expression(); echo $e->monthly(28); // 0 0 28 * * echo $e->weekly($e::FRIDAY)->at('05:30'); // 30 5 * * 5 echo $e->daily('06:10'); // 10 6 * * * echo Expression::create() // */5 0 16 1 5 ->setMinute('*/5') ->setHour('*') ->setDayOfMonth(16) ->setDayOfWeek('fri') ->setMonth('Jan'); // ------------ $testFunc = function () { echo 'TEST OK'; }; $scheduler = new Scheduler(); $scheduler ->addJob('* * * * *', function() { // just do something })->addJob('0 0 * * * *', $testFunc); $scheduler->run(); // ----------- $logPath = '/path/to/log.log'; $scheduler = new Scheduler('2021-07-05 06:10:00'); $scheduler->addJob($e, function () use($logPath) { // run standalone php script $cmd = "/usr/bin/php /path/to/script.php >> {$logPath} 2>&1"; system($cmd); }); $scheduler->run();