总共 2 步:
1. 增加配置项
2.开发运行脚本
运行:
php think check
配置里面是 checkOnline ,可以简写开头,如 php think check 或者 php think ch,只要没有重复的就能找到。
配置项位于 config/console.php,修改如下:
<?php
// +----------------------------------------------------------------------
// | 控制台配置
// +----------------------------------------------------------------------
return [
// 指令定义
'commands' => [
'checkOnline' => \app\command\OnlineChecker::class,
],
];
2. 写 app\command\OnlineChecker.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 think\facade\Db;
class OnlineChecker extends Command
{
/*
* think\console\Output
*/
var $output;
protected function configure()
{
// 指令配置
$this->setName('checkOnline')
->addArgument('name', Argument::OPTIONAL, "your name")
->addOption('city', null, Option::VALUE_REQUIRED, 'city name')
->setDescription('the hello command');
// // 指令配置
// $this->setName('check')
// ->setDescription('the check command');
}
protected function execute(Input $input, Output $output)
{
$this->output = $output;
// 指令输出
$this->log('检查设备在线');
$devices = Db::name("device")->select();
var_dump($devices);
}
protected function log($content)
{
if (is_array($content)) {
$content = json_encode($content, JSON_UNESCAPED_UNICODE);
}
$this->output->writeln(date('Y-m-d H:i:s') . ' ' . $content);
}
}