/** * 创建子进程执行定时任务 */ public function start() { $this->start_time = time(); $this->log('start. pid' . getmypid()); $manager = new ProcessManager(); $missions = $this->getMission(); foreach ($missions as $mission) { $mission_executor = new Mission($mission['cmd'], $mission['output']); $this->log($mission['cmd']); $user_name = isset($mission['user_name']) ? $mission['user_name'] : null; $group_name = isset($mission['group_name']) ? $mission['group_name'] : null; try { $manager->fork(new Process([$mission_executor, 'start'], $mission['name']), $user_name, $group_name); } catch (\Exception $e) { $this->log($e->getMessage()); } } //等待子进程退出 do { foreach ($manager->getChildren() as $process) { $iid = $process->getInternalId(); if ($process->isAlive()) { // echo sprintf('Process %s is running', $iid) . PHP_EOL; } else { if ($process->isFinished()) { // echo sprintf('Process %s is finished', $iid) . PHP_EOL; } } } sleep(1); } while ($manager->countAliveChildren()); $this->log('end. pid:' . getmypid()); }
/** * 创建子进程执行定时任务 */ public function start($time) { $this->start_time = $time; $this->logger->info("start. pid:" . getmypid()); $manager = new ProcessManager(); $missions = $this->getMission(); foreach ($missions as $mission) { $mission_executor = new Mission($mission['cmd'], $mission['output']); $this->logger->info("start cmd:" . $mission['cmd']); $user_name = isset($mission['user_name']) ? $mission['user_name'] : null; $group_name = isset($mission['group_name']) ? $mission['group_name'] : null; try { $manager->fork(new Process([$mission_executor, 'start'], $mission['name']), $user_name, $group_name); } catch (\Exception $e) { $this->logger->error($e->getMessage(), $e->getTraceAsString()); } } //等待子进程退出 do { sleep(1); } while ($manager->countAliveChildren()); $this->logger->info("end. pid:" . getmypid()); }