thinkphp 定时任务

1.先在application 下创建一个crontab/command文件夹,在文件夹中新建文件Test.php ,内容如下:

namespace app\crontab\command;

use think\console\Command;

use think\console\Input;

use think\console\Output;

use think\Db;

use think\Log;

class Test extends Command

{

    protected function configure(){

        $this->setName('Test')->setDescription("计划任务 Test");

    }

    //调用SendMessage 这个类时,会自动运行execute方法

    protected function execute(Input $input, Output $output){

        $output->writeln('Date Crontab job start...');

        /*** 这里写计划任务列表集 START ***/

        $output->writeln('这里可以调用控制器流程的代码');

        /*** 这里写计划任务列表集 END ***/

        $output->writeln('Date Crontab job end...');

    }

}


2.然后在application下的command.php 添加一个命令行:'app\crontab\command\Test'

return [

'app\crontab\command\Test'

];

3.在有环境变量的环境中执行命令(需要到项目根目录,与think目录同级):

php think Test     这个Test是与第一步的文件名相同

4.创建一个crontab 命令执行

*      *      *      *      /usr/local/php/bin/php  /www/wwwroot/xiangmu/think  Test

*     *       *      *     php的bin目录下的php   项目的根目录的think   创建的任务文件名

你可能感兴趣的:(thinkphp 定时任务)