使用ThinkPHP框架(thinkphp8.0)創(chuàng)建定時任的操作步驟
1、安裝定時任務(wù)composer包
composer require easy-task/easy-task
2、創(chuàng)建命令行處理類文件
php think make:command Task task
會生成文件: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')
//增加一個命令參數(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'); //是否強制停止
$task->stop($force);
}
else
{
exit('Command is not exist');
}
}
}3、配置config\console.php文件
<?php
// +----------------------------------------------------------------------
// | 控制臺配置
// +----------------------------------------------------------------------
return [
// 指令定義
'commands' => [
'task' => 'app\command\Task',
],
];4、執(zhí)行命令(windows請使用cmd):
php think task start 啟動命令 php think task status 查詢命令 php think task stop 關(guān)閉命令 php think task stop force 強制關(guān)閉命令
以上就是使用ThinkPHP框架(thinkphp8.0)創(chuàng)建定時任的操作步驟的詳細(xì)內(nèi)容,更多關(guān)于ThinkPHP框架創(chuàng)建定時任務(wù)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
PHP四種排序算法實現(xiàn)及效率分析【冒泡排序,插入排序,選擇排序和快速排序】
這篇文章主要介紹了PHP四種排序算法實現(xiàn)及效率分析,結(jié)合具體實例形式分析了php冒泡排序,插入排序,選擇排序和快速排序的具體定義、用法及算法復(fù)雜度分析,具有一定參考借鑒價值,需要的朋友可以參考下2018-04-04
PHP實現(xiàn)的下載遠(yuǎn)程圖片自定義函數(shù)分享
這篇文章主要介紹了PHP實現(xiàn)的下載遠(yuǎn)程圖片自定義函數(shù)分享,本文直接給出實現(xiàn)代碼和,本文直接給出實現(xiàn)代碼和使用方法,需要的朋友可以參考下2015-01-01
PHP實現(xiàn)獲取毫秒時間戳的方法【使用microtime()函數(shù)】
這篇文章主要介紹了PHP實現(xiàn)獲取毫秒時間戳的方法,結(jié)合實例形式分析了php使用microtime()函數(shù)獲取、轉(zhuǎn)換毫秒級時間戳的相關(guān)操作技巧,需要的朋友可以參考下2019-03-03

