windows组件下载:
https://download.csdn.net/download/qq_32421489/21698358
(1).安装tp6
composer create-project topthink/think tp
(2).安装定时任务composer包
composer require easy-task/easy-task
(3).创建命令行处理类文件
php think make:command Task task
会生成文件:tp\app\command\Task.php
将Task.php文件内容修改如下:
<?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; use EasyTask\Task as EasyTsk; 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 EasyTsk(); $task->setRunTimePath('./runtime/'); $task->addFunc(function () { // $url = 'https://www.baidu.com'; // file_get_contents($url); file_put_contents('./log.txt', '123'); }, 'request', 3, 1); // 根据命令执行 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'); } } }
(4).配置tp\config\console.php文件
<?php // +---------------------------------------------------------------------- // | 控制台配置 // +---------------------------------------------------------------------- return [ // 指令定义 'commands' => [ 'task' => 'app\command\Task', ], ];
(5).执行命令(windows请使用cmd):
php think task start 启动命令 php think task status 查询命令 php think task stop 关闭命令 php think task stop force 强制关闭命令