php – Symfony Process – 从服务执行命令

如何运行命令(app / console execute:my:command)
通过新流程提供服务?

我试试这个:

use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;

$process = new Process(
    'app/console execute:my:command'
);
$process->start();

但没有任何反应……
如果我通过终端手动调用它,它可以工作:

app/console execute:my:command

我究竟做错了什么?

EDIT – Solution: We need to write the whole path. In my case:

06002

$processString = sprintf(
    'php %s/../app/console %s %s',
    $this->kernelRootDir,
    self::MY_COMMAND,
    $myArgument
);

$process = new Process($processString);
$process->start();

if (!$process->isSuccessful()) {
    throw new ProcessFailedException($process);
}
最佳答案
这几乎是你真正需要做的.我总是设置工作目录并假设这是必需的,以便从项目的根目录运行Symfony命令

$process = new Process(
    'app/console execute:my:command'
);
$process->setWorkingDirectory(getcwd() . "../");

$process->start();

出于调试目的,我通常会设置

$process->setOptions(['suppress_errors' => false]);

同样

转载注明原文:php – Symfony Process – 从服务执行命令 - 代码日志