ThinkPHP 定时任务

在 application/admin/command  目录下新建一个 Test.php 文件

复制代码
namespace app\admin\command;

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

class Test extends Command
{
    protected function configure(){
        $this->setName('test')->setDescription("计划任务 Test");
    }
    // 调用Test 这个类时,自动调用 execute 这个方法 
    protected function execute(Input $input, Output $output){
        $output->writeln('Date Crontab job start...');
        /*** 这里写计划任务列表集 START ***/
 
        $this->test();
 
        /*** 这里写计划任务列表集 END ***/
        $output->writeln('Date Crontab job end...');
    }

    private function test()
    {
        echo '定时任务成功啦~';
    }

}
复制代码

修改 application/command.php

return [
    'app\admin\command\Test'
];

进入项目目录 (并非是根目录) ,使用命令查看

php think

运行定时任务

php think test

Liunx 中编辑crontab

# 编辑crontab
crontab -e

# 进入编辑模式后添加如下内容
* * * * * php /home/wwwroot/demo.com/think test    # 每分钟执行一次计划任务

# 保存退出后重启crontab
/etc/init.d/crond restart

效果图如下:

如果没有使用ThinkPHP 自身的定时任务,需要执行指定控制器,在Liunx 中编辑 crontab 

* * * * * /usr/bin/php /www/yoursite/public/index.php /tool/Crontab/execCrontab > /dev/null  2>&1 &

 

posted @   柔和的天空  阅读(1516)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示