symfony/process - 进程静默不启动


symfony/process - Process silently not starting

在一个新的symfony2项目中(安装如此处所述(,我想启动一个控制台进程作为请求的一部分。该应用程序运行在带有nginx + php-fpm的"标准"ubuntu 14.04盒子上。

请考虑以下控制器代码:

<?php
namespace AppBundle'Controller;

use Symfony'Bundle'FrameworkBundle'Controller'Controller;
use Symfony'Component'HttpFoundation'JsonResponse;
use Symfony'Component'Process'Process;
use Sensio'Bundle'FrameworkExtraBundle'Configuration'Route;
class CommandController extends Controller
{
    /**
     * @Route("/command")
     * @return JsonResponse
     */
    public function commandAction ()
    {
        $rootDir = $this->get('kernel')->getRootDir();
        $env = $this->get('kernel')->getEnvironment();
        $commandline = $rootDir . '/console --env=' . $env . ' acme:hello --who jojo'
        $process = new Process($commandline);
        $process->start();
        return new JsonResponse(array('command' => $commandline));
    }
}

当我向/command 发出请求时,我得到了预期的结果并且进程开始了,例如,我看到它带有 htop 等。当我再次发出此请求时,我得到了预期的结果,但要启动的过程没有显示在任何地方。没有错误,什么都没有。

重新启动 php5-fpm 服务使我能够通过请求再次启动一个进程,所以基本上我需要在每个请求后重新启动整个 php-service。所以这可能不是编程问题。但老实说,我还不知道。这个问题之前在堆栈溢出上描述过,Symfony2 - 进程启动 symfony2 命令,但 exec 的解决方法对我不起作用。

有人有线索吗?

谢谢,问候,乔乔

您的进程很可能在设法完成其工作之前就死了。这是因为 PHP 在响应返回到客户端并关闭连接后会杀死它。

Process::start()用于异步启动进程。您需要wait()才能完成它,或者检查它是否已完成isRunning()

$process->start();
$process->wait(function ($type, $buffer) {
    // do sth while you wait
});

或者,使用 Process::run() 而不是 Process:start()

如果要在后台处理某些内容,请使用消息队列。

> 2020 年只有一个更新....我收到一个无声错误,并这样做进行调试

$process = new Process($command);
$process->run();
print_r($process->getErrorOutput());