When I try to run that phing command: bin/phing clear_cache_action from a console, everything works. Unfortunately, when I try to run the same command from the controller in the Symfony project I get an error.
That my code:
public function clearAction()
{
$process = new Process('bin/phing clear_cache_action');
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
}
Symfony returns me that error:
The command "bin/phing clear_cache_action" failed.
Exit Code: 127(Command not found)
Working directory: /var/www/caolin/web
Output:
================
Error Output:
================
sh: 1: bin/phing: not found
Linux commands e.g. 'ls' works properly.
How can I run phing command from code?
I guess you are trying to execute phing from a Controller. Thus Working directory: /var/www/caolin/web instead of /var/www/caolin causes resolving bin/phing to /var/www/caolin/web/bin/phing which does not exist. You should set your current working directory to %kernel.project_dir%:
$process = new Process(
['bin/phing', 'clear_cache_action'],
$this->getParameter('kernel.project_dir')
);
$process->run();
However, I would not recommend starting a process from a Controller unless you are really sure what you are doing.
Related
I am developing a Laravel (8.83.24) app and testing with Laravel's built in server "php artisan serve". I am using Process and am trying to debug a memory allocation error which has lead me to here:
$process = new Process(['node', '-v']);
$process->run();
print_r($process);
That leads me to a line in the output:
[command] => cmd /V:ON /E:ON /D /C (node -v) 1>"C:\Users\mat\AppData\Local\Temp\sf_proc_01.out" 2>"C:\Users\mat\AppData\Local\Temp\sf_proc_01.err"
and sf_proc_01.err contains:
'node' is not recognized as an internal or external command,
operable program or batch file.
Clearly node can't be found. I am on Windows 10. Node is set in my in System and User PATH. Running "node -v" from cmd.exe works and returns the version number. So Laravel's server doesn't seem to be able to find node. However, I can't check the path as running this:
$process = new Process(['echo', '%PATH%']);
$process->run();
print_r($process);
Just leads to an output of
"%PATH%"
How can I make Node findable by Laravel / Process?
Many thanks for your help.
I eventually solved this by setting the path for node in my Laravel .env file:
NODE_PATH='C:\PROGRA~1\nodejs\node.exe'
NPM_PATH='D:\Work\Project\node_modules'
I'm trying to execute audit2allow using Symfony\Component\Process\Process.
When I run exec("audit2allow -a -M a2a"); in PHP, it works just fine, a2a.pp and a2a.te is produced.
$process = new Process(['audit2allow', '-a', '-M', 'a2a']);
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
However, the above code produces the output below
Symfony\Component\Process\Exception\ProcessFailedException : The command "'audit2allow' '-a' '-M' 'a2a'" failed.
Exit Code: 1(General error)
Working directory: /var/www/html/example
Output:
================
compilation failed:
a2a.te:6:ERROR 'syntax error' at token '' on line 6:
/usr/bin/checkmodule: error(s) encountered while parsing configuration
/usr/bin/checkmodule: loading policy configuration from a2a.te
which is the typical output when there is a empty /var/log/audit/audit.log.
What do I need to change to make it work properly? Symfony claims it is not a bug.
https://github.com/symfony/symfony/issues/35862
Update
Using Symfony\Component\Process\Process (code above) actually produces the file a2a.te, but it has only 1 line.
module a2a 1.0;
Whereas using exec() produces the file a2a.te with many lines:
module a2a 1.0;
require {
type kernel_t;
type vmblock_t;
type container_t;
...
Why does running the same command on Symfony\Component\Process\Process and exec() gives different outcomes?
I've installed mjml cli using the following command (as described in the mjml documentation):
npm install mjml --save
now if i did node_modules/.bin/mjml in the command line it will run successfully.
the problem is when i use the symfony process component in php i got the following error (even if it's the right path):
The command "/Users/qoraiche/Documents/my-app/node_modules/.bin/mjml" failed. Exit Code: 127(Command not found) Working directory: /Users/qoraicheOS/Documents/my-app/public Output: ================ Error Output: ================ env: node: No such file or directory
Symfony process code:
$process = new Process(base_path('node_modules/.bin/mjml'));
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
By the way i have installed mjml globally as well and try it with no luck.
Try providing the full path to node.js when calling the MJML binary from a Symfony Process().
$process = new Process('/your/path/to/node ' . base_path('node_modules/.bin/mjml'));
I use this method in my own apps. Of course, you will need to provide the input and output files to actually transpile anything.
I have some script: /home/user/project/deploy.sh
I trying to run with Symfony/Process component from php handler in another directory:
$process = new Process(['./deploy.sh']);
$process->setWorkingDirectory('/home/user/project');
$process->run(function ($type, $buffer) {
echo $buffer;
});
But I got an error:
The provided cwd "/home/user/project" does not exist.
This folder exists and symfony handler ran as correct user which have correct permissions to this folder. What is the correct way to run this script from another folder?
Just for debugging purposes test code below.
$process = new Process(['/home/user/project/bin/console', '--version']);
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
If it does work then try:
new Process(['/home/user/project/deploy.sh']);
new Process(['sh /home/user/project/deploy.sh']);
Process::fromShellCommandline('/home/user/project/deploy.sh');
You may need to use the full path to sh since the webserver uses a different user and a PATH variable.
I have the following simple command:
$process = new Process("php /Users/Name/Sites/App/app/../bin/console cache:clear --env=prod");
$process->run();
when I try to run this it gives me:
string(153) " Parse error: parse error in /Users/Name/Sites/App/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php on line 278 "
What is wrong?
Looks like you are using Symfony 3.0 that has "finally"
https://github.com/symfony/symfony/blob/3.0/src/Symfony/Component/DependencyInjection/Container.php#L282
PHP 5.5 and later has support for "finally" in try/catch blocks.
http://php.net/manual/en/language.exceptions.php
And looks like your php version is less then 5.5, so upgrade your php version > 5.5 and it will work
$process = new Process("php /Users/Name/Sites/App/app/../bin/console cache:clear --env=prod");
$process->run();
You should try
$process = new Process("php /Users/Name/Sites/App/app/console cache:clear --env=prod");
$process->run();
Because console is in app/ directory and not in bin/