使用ThinkPHP框架(thinkphp8.0)創(chuàng)建定時(shí)任的操作步驟
1、安裝定時(shí)任務(wù)composer包
composer require easy-task/easy-task
2、創(chuàng)建命令行處理類文件
php think make:command Task task
會(huì)生成文件:app\command\Task.php
將Task.php文件內(nèi)容修改如下:
<?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() { //設(shè)置名稱為task $this->setName('task') //增加一個(gè)命令參數(shù) ->addArgument('action', Argument::OPTIONAL, "action", '') ->addArgument('force', Argument::OPTIONAL, "force", ''); } protected function execute(Input $input, Output $output) { //獲取輸入?yún)?shù) $action = trim($input->getArgument('action')); $force = trim($input->getArgument('force')); // 配置任務(wù),每隔20秒訪問2次網(wǎng)站 $task = new \EasyTask\Task(); $task->setRunTimePath('./runtime/'); $task->addFunc(function () { $url = 'https://www.wushengyong.com/'; file_get_contents($url); }, 'request', 20, 2);; // 根據(jù)命令執(zhí)行 if ($action == 'start') { $task->start(); } elseif ($action == 'status') { $task->status(); } elseif ($action == 'stop') { $force = ($force == 'force'); //是否強(qiáng)制停止 $task->stop($force); } else { exit('Command is not exist'); } } }
3、配置config\console.php文件
<?php // +---------------------------------------------------------------------- // | 控制臺(tái)配置 // +---------------------------------------------------------------------- return [ // 指令定義 'commands' => [ 'task' => 'app\command\Task', ], ];
4、執(zhí)行命令(windows請(qǐng)使用cmd):
php think task start 啟動(dòng)命令 php think task status 查詢命令 php think task stop 關(guān)閉命令 php think task stop force 強(qiáng)制關(guān)閉命令
以上就是使用ThinkPHP框架(thinkphp8.0)創(chuàng)建定時(shí)任的操作步驟的詳細(xì)內(nèi)容,更多關(guān)于ThinkPHP框架創(chuàng)建定時(shí)任務(wù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
PHP使用PHPexcel導(dǎo)入導(dǎo)出數(shù)據(jù)的方法
這篇文章主要介紹了PHP使用PHPexcel導(dǎo)入導(dǎo)出數(shù)據(jù)的方法,以實(shí)例形式較為詳細(xì)的分析了PHP使用PHPexcel實(shí)現(xiàn)數(shù)據(jù)的導(dǎo)入與導(dǎo)出操作相關(guān)技巧,需要的朋友可以參考下2015-11-11為你總結(jié)一些php系統(tǒng)類函數(shù)
這篇文章是小編為大家總結(jié)的一些php系統(tǒng)類函數(shù),很實(shí)用,需要的朋友千萬(wàn)不要錯(cuò)過(guò)。2015-10-10PHP開發(fā)api接口安全驗(yàn)證操作實(shí)例詳解
這篇文章主要介紹了PHP開發(fā)api接口安全驗(yàn)證操作,結(jié)合實(shí)例形式詳細(xì)分析了PHP開發(fā)api接口安全驗(yàn)證的原理、操作技巧與注意事項(xiàng),需要的朋友可以參考下2020-03-03PHPCMS遭遇會(huì)員投稿審核無(wú)效的解決方法
本站的投稿平臺(tái)突然遇到會(huì)員投稿之后,主編在后臺(tái)審核功能失效的問題,在網(wǎng)站上搜了半天也沒找到方法,先把小編的處理過(guò)程記錄下來(lái),分享給大家2017-01-01ThinkPHP Where 條件中常用表達(dá)式示例(詳解)
下面小編就為大家?guī)?lái)一篇ThinkPHP Where 條件中常用表達(dá)式示例(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03php實(shí)現(xiàn)的驗(yàn)證碼文件類實(shí)例
這篇文章主要介紹了php實(shí)現(xiàn)的驗(yàn)證碼文件類,實(shí)例分析了php生成驗(yàn)證碼文件的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-06-06php基礎(chǔ)知識(shí):類與對(duì)象(3) 構(gòu)造函數(shù)和析構(gòu)函數(shù)
php基礎(chǔ)知識(shí):類與對(duì)象(3) 構(gòu)造函數(shù)和析構(gòu)函數(shù)...2006-12-12