anxu/crontab

为 yii2 框架提供的 crontab

v0.1 2017-12-13 10:07 UTC

This package is auto-updated.

Last update: 2024-09-11 18:56:15 UTC


README

Yii2 插件,用于管理所有后台定时任务,无需修改 crontab

注意

无法在 Windows 系统服务器上运行

用法

通过 composer 安装

composer require anxu/crontab:"*"

Yii2 控制台脚本,示例如下:

namespace app\commands;

use yii\console\Controller;
use anxu\Crontab\Crontab;

class DemoController extends Controller
{
    public function actionIndex()
    {
        // 从数据库获取执行的任务
        $jobs=[
            [
                'name'=>'1',
                'schedule'=>'* * * * *',
                'command'=>'date',
                'output'=>'log/log.log'
            ],[
                'name'=>'2',
                'schedule'=>'* * * * *',
                'command'=>'echo "test2"',
                'output'=>'log/log.log'
            ]
        ];

        $a  = new Crontab();
        $a->add($jobs);
        $a->run();
    }
}

将上述脚本添加到 crontab 中,如下所示:

* * * * * /path/to/project/yii demo/index 1>> /dev/null 2>&1

最后,启动 crontab

使用 "Y-m-d H:i:s" 格式,后面的 秒s 将会被忽略;