4

In a fresh symfony2-project (installation as described here), I would like to start a console-process as part of a request. The app runs on a "standard" ubuntu 14.04 box with nginx + php-fpm.

Consider this controller-code:

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

When I issue a request to /command, I get my expected result and the process starts, e.g. I see it with htop and the like. When I issue this request again, I get my expected result, but the process to be started does not show up anywhere. No errors, no nothing.

Restarting the php5-fpm service enables me to start one process through a request again, so basically I need to restart the whole php-service after each request. So this maybe is no programming-issue. But I don't know yet, honestly. The problem was described on stackoverflow before, Symfony2 - process launching a symfony2 command, but the workaround with exec is not working for me.

Does somebody have a clue?

Thanks, regards, jojo

4

2 回答 2

10

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

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

$process->start();

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

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

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

于 2014-12-22T19:14:18.277 回答
0

2020 年只有一次更新 .... 我收到一个无声错误,并这样做是为了调试

$process = new Process($command);
$process->run();
print_r($process->getErrorOutput());
于 2020-07-02T20:33:50.167 回答