无法在Symfony中运行任何进程命令

时间:2020-07-18 19:27:51

标签: php shell symfony symfony5

我正在尝试在Symfony 5.1中运行linux进程,如下所述: https://symfony.com/doc/current/components/process.html

use Symfony\Component\Process\Process;

(...)

$command = 'echo hello';
$process = new Process([$command]);

$process->start();

foreach ($process as $type => $data) {
    if ($process::OUT === $type) {
        echo "\nRead from stdout: ".$data;
    } else { // $process::ERR === $type
        echo "\nRead from stderr: ".$data;
    }
}

无论我的命令行是什么,我都会得到以下输出:

Read from stderr: sh: 1: exec: echo hello: not found

1 个答案:

答案 0 :(得分:4)

在您提到的文档中,您可以看到示例:https://symfony.com/doc/current/components/process.html#usage

$process = new Process(['ls', '-lsa']);

Process构造函数的源代码: https://github.com/symfony/symfony/blob/5.1/src/Symfony/Component/Process/Process.php#L132

第一个参数是:

* @param array          $command The command to run and its arguments listed as separate entries

尝试$process = new Process(['echo', 'hello']);而不是$process = new Process(['echo hello']);