(一)Window10创建PHP定时任务
1.1 前言
- 项目:thinkphp项目
- 项目路径:
D:\phpstudy_pro\WWW\Codes\Web\PHP\gameAdmin
1.2 创建 命令任务
在app目录下创建commond目录,然后创建控制器文件(比如Server.php)
<?php
declare (strict_types = 1);
namespace app\command;
use catchAdmin\server\model\ServerList;
use think\console\Command;
use think\console\Input;
use think\console\Output;
/**
* 排行榜定时任务
* Class Rank
* @package app\command
*/
class Server extends Command
{
protected function configure()
{
// 指令配置
// setName()设置指令名称
// setDescription()设置指令描述
$this->setName('check_server_advice')
->setDescription('the check_server_advice command');
}
protected function execute(Input $input, Output $output)
{
// 指令输出
$time =date("Y-m-d H:i:s");
//指令输出信息
$output->writeln('检查服务器列表数据就要开始了!');
// //验证服务器是否正常
$task = new ServerList();
$res=$task->autoCheckService();
$output->writeln($res);
$output->writeln('检查服务器列表数据已经操作成功了!');
$output->writeln("操作时间为: $time ");
}
}
配置指定
路径地址:config/console.php
<?php
// +----------------------------------------------------------------------
// | 控制台配置
// +----------------------------------------------------------------------
return [
// 指令定义
'commands' => [
'check_server_advice' => '\app\command\Server', //php think check_server_advice
],
];
1.3 创建任务计划
- 创建
server_cron.bat
文件放在项目跟目录 - 将以下内容复制到
server_cron.bat
文件中
::进入D目录
D:
::进入项目目录
cd D:\phpstudy_pro\WWW\Codes\Web\PHP\gameAdmin
::执行服务器列表异常检测php命令
php think check_server_advice
-
window10系统下搜索
任务计划
-
快捷打开:
win+R
输入taskschd.msc
-
创建任务