php – 从Symfony命令运行Linux命令

weixin_38056873 2019-09-12 12:03:13
如何在Symfony命令中运行简单的Linux命令? 例如.我想在命令末尾运行ssh username @ host -p port … 我试过了: $input = new StringInput('ssh username@host -p port'); $this->getApplication()->run($input, $output); 但是这引发了以下异常:`“ – p”选项不存在.` 它似乎是在我的Symfony命令的相同“上下文”中执行的.
...全文
70 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38066613 2019-09-12
  • 打赏
  • 举报
回复
How can I run a simple Linux command in a Symfony command? 首先,尝试执行简单/普通命令(ls)以查看发生的情况,然后转到您的特殊命令. http://symfony.com/doc/current/components/process.html 码: 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’);这也是我有时做的.例如shell_exec(‘git ls-remote url-to-my-git-project-repo master’);

437

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧