Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Linux command from Symfony command

How can I run a simple Linux command in a Symfony command?

E.g. I want to run ssh username@host -p port at the end of the command...

I've tried:

$input = new StringInput('ssh username@host -p port');
$this->getApplication()->run($input, $output);

But that throws the following exception: `The "-p" option does not exist.``

It seems to be executed in the same "context" of my Symfony command.

like image 304
Maarten Troonbeeckx Avatar asked Mar 06 '17 14:03

Maarten Troonbeeckx


People also ask

What is Symfony CMD?

symfony-cmd is a part of Symfony Flex. Your composer. json does not contain any requirement for Flex, so running composer require symfony/flex might resolve that problem. Follow this answer to receive notifications. answered Aug 29, 2021 at 15:16.

What is bin console in Symfony?

The Symfony framework provides lots of commands through the bin/console script (e.g. the well-known bin/console cache:clear command). These commands are created with the Console component. You can also use it to create your own commands.

What is Symfony Console component?

The Console component eases the creation of beautiful and testable command line interfaces. The Console component allows you to create command-line commands. Your console commands can be used for any recurring task, such as cronjobs, imports, or other batch jobs.

What is Symphony process?

Process Management Software. Symphony allows you and your team to create well-structured work routines and automate recurring processes to increase productivity and team communication. Start using Symphony. Start using Symphony.


2 Answers

How can I run a simple Linux command in a Symfony command?

First of all, try to execute a simple/plain command (ls) to see what happens, then move on to your special command.

http://symfony.com/doc/current/components/process.html

CODE:

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();

RESULT:

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

As you can see above, if you put the piece of code in one of your controllers for testing purposes, ls -lsa lists files/folders stored under web folder!!!

You can just do shell_exec('ls -lsa'); as well which is what I sometimes do. e.g. shell_exec('git ls-remote url-to-my-git-project-repo master');

like image 77
BentCoder Avatar answered Oct 24 '22 10:10

BentCoder


This is the updated interface, used in Symfony 5.2. The process constructor now requires an array as input.

source: https://symfony.com/doc/current/components/process.html

The Symfony\Component\Process\Process class executes a command in a sub-process, taking care of the differences between operating system and escaping arguments to prevent security issues. It replaces PHP functions like exec, passthru, shell_exec and system

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

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

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

echo $process->getOutput();
like image 1
Aris Avatar answered Oct 24 '22 08:10

Aris