1、安装定时任务composer包
1 | composer require easy-task /easy-task |
2、创建命令行处理类文件
1 | php think make : command Task task |
会生成文件:appcommandTask.php
将Task.php文件内容修改如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | setName( 'task' ) //增加一个命令参数 ->addArgument( 'action' , Argument::OPTIONAL, "action" , '' ) ->addArgument( 'force' , Argument::OPTIONAL, "force" , '' ); } protected function execute(Input $input , Output $output ) { //获取输入参数 $action = trim( $input ->getArgument( 'action' )); $force = trim( $input ->getArgument( 'force' )); // 配置任务,每隔20秒访问2次网站 $task = new EasyTaskTask(); $task ->setRunTimePath( './runtime/' ); $task ->addFunc( function () { file_get_contents ( $url ); }, 'request' , 20, 2);; // 根据命令执行 if ( $action == 'start' ) { $task ->start(); } elseif ( $action == 'status' ) { $task ->status(); } elseif ( $action == 'stop' ) { $force = ( $force == 'force' ); //是否强制停止 $task ->stop( $force ); } else { exit ( 'Command is not exist' ); } } } |
3、配置configconsole.php文件
1 2 3 4 | [ 'task' => 'appcommandTask' , ], ]; |
4、执行命令(windows请使用cmd):
1 2 3 4 | php think task start 启动命令 php think task status 查询命令 php think task stop 关闭命令 php think task stop force 强制关闭命令 |
以上就是使用ThinkPHP框架(thinkphp8.0)创建定时任的操作步骤的详细内容,更多关于ThinkPHP框架创建定时任务的资料请关注IT俱乐部其它相关文章!