Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从Symfony命令运行Linux命令_Php_Linux_Symfony_Command Line Interface - Fatal编程技术网

Php 从Symfony命令运行Linux命令

Php 从Symfony命令运行Linux命令,php,linux,symfony,command-line-interface,Php,Linux,Symfony,Command Line Interface,如何在Symfony命令中运行简单的Linux命令 例如,我想运行sshusername@host-p端口在命令末尾 我试过: $input = new StringInput('ssh username@host -p port'); $this->getApplication()->run($input, $output); 但这会引发以下异常:`the“-p”选项不存在`` 它似乎是在与Symfony命令相同的“上下文”中执行的。据我所知,而且我对Symfony了解不多,您必

如何在Symfony命令中运行简单的Linux命令

例如,我想运行
sshusername@host-p端口
在命令末尾

我试过:

$input = new StringInput('ssh username@host -p port');
$this->getApplication()->run($input, $output);
但这会引发以下异常:`the“-p”选项不存在``


它似乎是在与Symfony命令相同的“上下文”中执行的。

据我所知,而且我对Symfony了解不多,您必须在之前指定选项username@host. 请在此处查看:

就你而言:

'ssh -p port username@host'
如何在Symfony命令中运行简单的Linux命令

首先,尝试执行一个简单/简单的命令(
ls
),看看会发生什么,然后继续执行您的特殊命令

代码:

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

$process = new Process('ls -lsa');
$process->run();

// executes after the command finishes
if (!$process->isSuccessful()) {
    throw new ProcessFailedException($process);
}

echo $process->getOutput();
total 36
4 drwxrwxr-x  4 me me 4096 Jun 13  2016 .
4 drwxrwxr-x 16 me me 4096 Mar  2 09:45 ..
4 -rw-rw-r--  1 me me 2617 Feb 19  2015 .htaccess
4 -rw-rw-r--  1 me me 1203 Jun 13  2016 app.php
4 -rw-rw-r--  1 me me 1240 Jun 13  2016 app_dev.php
4 -rw-rw-r--  1 me me 1229 Jun 13  2016 app_test.php
4 drwxrwxr-x  2 me me 4096 Mar  2 17:05 bundles
4 drwxrwxr-x  2 me me 4096 Jul 24  2015 css
4 -rw-rw-r--  1 me me  106 Feb 19  2015 robots.txt
结果:

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

$process = new Process('ls -lsa');
$process->run();

// executes after the command finishes
if (!$process->isSuccessful()) {
    throw new ProcessFailedException($process);
}

echo $process->getOutput();
total 36
4 drwxrwxr-x  4 me me 4096 Jun 13  2016 .
4 drwxrwxr-x 16 me me 4096 Mar  2 09:45 ..
4 -rw-rw-r--  1 me me 2617 Feb 19  2015 .htaccess
4 -rw-rw-r--  1 me me 1203 Jun 13  2016 app.php
4 -rw-rw-r--  1 me me 1240 Jun 13  2016 app_dev.php
4 -rw-rw-r--  1 me me 1229 Jun 13  2016 app_test.php
4 drwxrwxr-x  2 me me 4096 Mar  2 17:05 bundles
4 drwxrwxr-x  2 me me 4096 Jul 24  2015 css
4 -rw-rw-r--  1 me me  106 Feb 19  2015 robots.txt
如上所述,如果出于测试目的将代码放入一个控制器中,
ls-lsa
列出存储在
web
文件夹下的文件/文件夹


您只需执行
shell_exec('ls-lsa')也是我有时做的事情。e、 g.
shell_exec('git ls到我的git项目repo master的远程url')

这是Symfony 5.2中使用的更新界面。进程构造函数现在需要一个数组作为输入

资料来源:

Symfony\Component\Process\Process类在 子进程,考虑操作系统之间的差异 以及逃避争论以防止安全问题。它取代了PHP exec、PASSTRU、shell_exec和system等功能