Croon是一个PHP版本的CronTab实现
之前也做过一个类似的实现php-crontab,Croon与之不同的是:使用进程管理方式Fork工作进程;有友好的日志模块;加入事件驱动;更佳规范和稳定
- 兼容CronTab语法
- 精确到秒级控制
- PSR标准
- 支持事件绑定
- 支持日志
- PHP 5.3.9+
- ext-pcntl
- ext-posix
- Composer
库依赖(使用composer install
自动安装)
下载使用:
Git:
git clone git://github.com/hfcorriez/croon.git
cd croon
composer install
Composer
composer require croon/croon
croon.list
* * * * * * ls -l >> /tmp/ls.log
# 兼容系统crontab
* * * * * pwd >> /tmp/pwd.log
执行
./bin/croon croon.list -l croon.log
croon.log
[2013-04-20 14:07:01] 27a6c9 - debug - Croon...!!!
[2013-04-20 14:07:01] 27a6c9 - info - Execute (ls >> /tmp/ls.log)
[2013-04-20 14:07:01] 27a6c9 - info - Finish (ls >> /tmp/ls.log)[0]
- 修改数据库连接信息 bin/croon_with_mysql
- 表结构为
+---------------------+--------------------------------------------------------+
| time | command |
+---------------------+--------------------------------------------------------+
| [秒] 分 时 日 月 周 | command |
+---------------------+--------------------------------------------------------+
执行
./bin/croon_with_mysql -l croon.log
bootstrap.php
<?php
// 绑定启动事件
$croon->on('run', function() use($croon) {
// 注入db
$croon->db = new \PDO('mysql://localhost:3306;dbname=reports');
});
// 绑定执行事件
$croon->on('executed', function ($command, $output) use ($croon) {
// 记录执行结果
$croon->db->exec(sprintf(
'INSERT INTO cron(command, status, stdout, stderr, create_time) VALUES ("%s", "%s", "%s", "%s", "%s")',
$command, $output[0], $output[1], $output[2], date('Y-m-d H:i:s'))
);
});
执行
./bin/croon croon.list -l croon.log -b bootstrap.php
(The MIT License)
Copyright (c) 2012 hfcorriez <hfcorriez@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.