1、安装定时任务composer包 [code]composer require easy-task/easy-task[/code]2、创建下令行处置惩罚类文件 [code]php think make:command Task task[/code]会天生文件:app\command\Task.php 将Task.php文件内容修改如下: [code]<?php declare (strict_types=1); namespace app\command; use think\console\Command; use think\console\Input; use think\console\input\Argument; use think\console\input\Option; use think\console\Output; class Task extends Command { protected function configure() { //设置名称为task $this->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 \EasyTask\Task(); $task->setRunTimePath('./runtime/'); $task->addFunc(function () { $url = 'https://www.wushengyong.com/'; 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'); } } }[/code]3、设置config\console.php文件 [code]<?php // +---------------------------------------------------------------------- // | 控制台设置 // +---------------------------------------------------------------------- return [ // 指令定义 'commands' => [ 'task' => 'app\command\Task', ], ];[/code]4、实行下令(windows请利用cmd): [code]php think task start 启动下令 php think task status 查询下令 php think task stop 关闭下令 php think task stop force 逼迫关闭下令[/code]以上就是利用ThinkPHP框架(thinkphp8.0)创建定时任的操纵步骤的详细内容,更多关于ThinkPHP框架创建定时任务的资料请关注脚本之家其它相干文章! 来源:https://www.jb51.net/program/313218qca.htm 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
|手机版|小黑屋|梦想之都-俊月星空
( 粤ICP备18056059号 )|网站地图
GMT+8, 2025-7-1 20:57 , Processed in 0.035815 second(s), 18 queries .
Powered by Mxzdjyxk! X3.5
© 2001-2025 Discuz! Team.