ghaziksibi
ghaziksibi

Reputation: 471

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

Upvotes: 4

Views: 2690

Answers (1)

Miro
Miro

Reputation: 1899

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);

Upvotes: 7

Related Questions