# TP5实战源码 --- 命令行 在开发中 我们经常会用到处理批量任务 定时任务 统计报表等等,这时命令行是一个简单有效的解决办法 ### 首先在base\command目录先建立一个命令行 类文件 Mikkle 名字你自己定 ~~~ <?php namespace app\base\command; use think\Cache; use think\console\Command; use think\console\Input; use think\console\Output; /** * Created by PhpStorm. * Power by Mikkle * QQ:776329498 * Date: 2017/6/12 * Time: 15:07 */ class Mikkle extends Command { protected $sleep = 3; protected function configure() { $this->setName('mikkle')->setDescription('Here is the mikkle\'s command '); } protected function execute(Input $input, Output $output) { while(true){ $output->writeln(json_encode($this->checkDo())); sleep($this->sleep); } } protected function checkDo(){ $state = false; $int = Cache::inc("Command",1); //这里根据查询的缓存内容 或者时间 写你的逻辑代码 //---------------- // you code //-------------- return $int; } } ~~~ ### 在command配置文件中 添加上 ~~~ return [ 'app\base\command\Mikkle', ]; ~~~ ### 命令行启动命令 * 首先进入你的项目目录 ~~~ cd /www/web/mikkle ~~~ * 运行 think mikkle ~~~ /www/wdlinux/php/bin/php think mikkle ~~~ ![](https://box.kancloud.cn/74f961ae343f792fa5cbf9a76021e299_471x176.png) > 因为我后端开启了服务 演示的时候就自动加2了