Stop the Symfony Console Team

I have a symfony console command that is continuously cycling

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 would like to stop it from the controller

    public function stopAction()
{ 
    posix_kill(posix_getpid(), SIGHUP);

    return new Response('ok');
}

but I do not know why it does not work.

+4
source share
1 answer

This probably won't work because the console command is launched in a different process than the action of the controller. Try saving the console command PID number to a file at the start of execution with something like:

file_put_contents("/tmp/console_command.pid", posix_getpid());

and then use this code in the controller:

posix_kill(file_get_contents("/tmp/console_command.pid"), SIGHUP);
+6
source

All Articles