67 Star 228 Fork 52

一个作词家/cron-manager

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

如何在thinkphp5中使用cron-manager

自定义命令行

对自定义命令行感兴趣的可以去看 thinkphp5官方手册

第一步,安装最新的cron-manager

composer require godv/cron-manager

第二步, 配置TP5项目的 application/command.php 文件

<?php
return [
    'app\cron\command\Cron',
];

第三步, 创建cron命令文件, 没有就手动创建 application/cron/Cron.php

<?php
namespace app\cron\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;

use think\console\input\Argument;
use think\console\input\Option;

class Cron extends Command
{
    protected function configure()
    {
        $this->addArgument('param', Argument::OPTIONAL);//查看状态
        // 设置命令名称
        $this->setName('cron')->setDescription('this is a supercron!');
    }

    protected function execute(Input $input, Output $output)
    {
        
        //获取参数值
        $args = $input->getArguments();
       
        $manager = new \SuperCronManager\CronManager();
        // 守护进程方式启动
        $manager->daemon = true;
        $manager->argv = $args['param'];

        // crontab格式解析
        $manager->taskInterval('每个小时的1,3,5分钟时运行一次', '1,3,5 * * * *', function(){
            echo "每个小时的1,3,5分钟时运行一次\n";
        });

        $manager->taskInterval('每1分钟运行一次', '*/1 * * * *', function(){
            echo "每1分钟运行一次\n";
        });

        $manager->taskInterval('每天凌晨运行', '0 0 * * *', function(){
            echo "每天凌晨运行\n";
        });

        $manager->taskInterval('每秒运行一次', 's@1', function(){
            echo "每秒运行一次\n";
        });

        $manager->taskInterval('每分钟运行一次', 'i@1', function(){
            echo "每分钟运行一次\n";
        });

        $manager->taskInterval('每小时钟运行一次', 'h@1', function(){
            echo "每小时运行一次\n";
        });

        $manager->taskInterval('指定每天00:00点运行', 'at@00:00', function(){
            echo "指定每天00:00点运行\n";
        });

        $manager->run();
    }
}

大功告成,开始使用

运行 (进入tp5根目录)

php think cron 

命令列表

php think cron stop 停止|restart 重启|status 任务状态|worker 进程状态|check 检查环境

马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/jianglibin/cron-manager.git
git@gitee.com:jianglibin/cron-manager.git
jianglibin
cron-manager
cron-manager
master

搜索帮助