Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop a Symfony Console command

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

like image 700
ghaziksibi Avatar asked Mar 12 '23 17:03

ghaziksibi


1 Answers

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);
like image 66
Miro Avatar answered Apr 06 '23 04:04

Miro