I have a Symfony console command that loops continuously
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;
}
I wish I could stop him from a controller
public function stopAction()
{
posix_kill(posix_getpid(), SIGHUP);
return new Response('ok');
}
but I do not know why it does not work
It probably does not work, because console command is running in different process than controller action. Try to store PID number of console command into the file at the beginning of execution with something like:
file_put_contents("/tmp/console_command.pid", posix_getpid());
and then use this code in controller:
posix_kill(file_get_contents("/tmp/console_command.pid"), SIGHUP);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With