gha*_*ibi 4 console command symfony
我有一个连续循环的Symfony控制台命令
protected function execute(InputInterface $input, OutputInterface $output)
{
//...
pcntl_signal(SIGHUP, [$this, 'stopCommand']);
$this->shouldStop = false;
while (true) {
pcntl_signal_dispatch();
if ($this->shouldStop) {
break;
}
sleep(60);
}
}
protected function stopCommand()
{
$this->shouldStop = true;
}
Run Code Online (Sandbox Code Playgroud)
我希望我能阻止他离开控制器
public function stopAction()
{
posix_kill(posix_getpid(), SIGHUP);
return new Response('ok');
}
Run Code Online (Sandbox Code Playgroud)
但我不知道为什么它不起作用
它可能不起作用,因为控制台命令在与控制器操作不同的进程中运行.尝试在执行开始时将控制台命令的PID编号存储到文件中,例如:
file_put_contents("/tmp/console_command.pid", posix_getpid());
Run Code Online (Sandbox Code Playgroud)
然后在控制器中使用此代码:
posix_kill(file_get_contents("/tmp/console_command.pid"), SIGHUP);
Run Code Online (Sandbox Code Playgroud)