2016-04-03 151 views
4

我有一个不断循环停止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; 
} 

我希望我可以从控制器

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

    return new Response('ok'); 
} 

阻止他,但我不知道为什么它不工作Symfony的控制台命令

回答

6

它可能不起作用,因为控制台命令运行在与控制器操作不同的进程中。尝试在开始执行存储控制台命令的PID号到文件的东西,如:

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

,然后在控制器使用此代码:

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