['name' => '测试', 'time' => 300], ]; protected function configure() { // 指令配置 $this->setName('autoTask') ->setDescription('自动程序'); } protected function execute(Input $input, Output $output) { go(function () { while (true) { foreach ($this->task as $k => $v) { $last_time = Cache::store('redis')->get($k . '_time', 0); if ($last_time + $v['time'] < time()) { try { $this->$k(); $this->log($v['name'] . '执行'); } catch (\Throwable $e) { Log::error($v['name'] . '运行出错:' . $e->getMessage() . '[' . $e->getFile() . ':' . $e->getLine() . ']'); $this->log($v['name'] . '运行出错:' . $e->getMessage()); } Cache::store('redis')->set($k . '_time', time()); } } Coroutine::sleep(1); } }); $output->info("自动程序启动"); } /** * @throws Exception */ private function test() { } public function log($msg) { echo "[" . date('Y-m-d H:i:s') . "] " . $msg . PHP_EOL; } }