Python Shell script not called in Symfony - php

I am using Ubuntu 18.04 and I have implemented openFace library which works fine through terminal that takes two input parameter containing name of images
Terminal command
python /home/machine/openface/demos/compare_two_pic.py {/home/machine/openface/demos/images/5G5X0dnI5xkD.jpg,/home/machine/openface/demos/images/orange.jpg}
I just pasted this command in Symfony but i didn't get any result.
$process = new Process(['python2', '/home/machine/openface/demos/compare_two_pic.py {/home/machine/openface/demos/images/5G5X0dnI5xkD.jpg,/home/machine/openface/demos/images/orange.jpg}',$arg(optional)]);
$process->run();
echo "here".$process->getOutput();
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
I make a web service to just call a function and check. Is this function working or not?
I have put this comment
$process = Process::fromShellCommandline('python2 /home/machine/openface/demos/compare_two_pic.py {/home/machine/openface/demos/images/5G5X0dnI5xkD.jpg,/home/machine/openface/demos/images/orange.jpg}');

Related

use Java command in laravel

I want to use java command in laravel project but I get error "'java' is not recognized as an internal or external command, operable program or batch file."
please help me
$process = new Process(['java','-cp', 'weka.jar weka.clusterers.SimpleKMeans', '-T', $path.'unseen.arff', '-l', $path.'Tourist.model']);
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
$output = $process->getOutput();
You are trying to call a Java jar file from PHP, you need a PHPBridge to access the java file. Here: PHP Java Bridge

Laravel Queue download file using CURL

I'm trying to generate the file using curl. When I try executing the command via terminal it works fine.
But when I try to implement it on laravel using a queue, the script runs but the downloaded file is corrupt.
As you can see from the "Time Spent" it doesn't even have value compared to the one executed from terminal. And the file is there but obviously corrupt since laravel already finished the command even before it started.
Here's my code on the handle() function of GeneratBukuVot Job:
$path = Storage::path('public/test.pdf');
$pentaho = 'http://192.168.0.17:8080/pentaho/api/repos/:home:admin:anggaran_hasil_and_belanja_param.prpt/generatedContent?output-target=pageable/pdf&search_year=2019';
$cmd = 'curl -u"admin:password" "'.$pentaho.'" -o "'.$path.'"';
$output = shell_exec($cmd);
I'm using Laravel version 5.8. Please, help.

Run .sh file using exec Laravel PHP

I am trying to run a .sh file that will import a excel file to my database. Both files are in same directory inside the public folder. For some reason the exec command isn't being executed or neither any error occurs.
.sh file colde:
IFS=,
while read column1
do
echo "SQL COMMAND GOES HERE"
done < file.csv | mysql --user='myusername' --password='mypassword' -D databasename;
echo "finish"
In my php file i have tried following:
$content = file_get_contents('folder_name/file_name.sh');
echo exec($content);
And:
shell_exec('sh /folder_name/file_name.sh');
Note: I can directly execute the sh file from gitbash however I want it to be done using function in Laravel controller. I'm using windows OS.
you can use Process Component of Symfony that is already in Laravel http://symfony.com/doc/current/components/process.html
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
$process = new Process('sh /folder_name/file_name.sh');
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
All of these answers are outdated now, instead use (Symfony 4.2 or higher):
$process = Process::fromShellCommandline('/deploy.sh');
Or
$process = new Process(['/deploy.sh']);
https://symfony.com/doc/current/components/process.html
I know this is a little late but I can't add a comment (due to being a new member) but to fix the issue in Windows " 'sh' is not recognized as an internal or external command, operable program or batch file." I had to change the new process from:
$process = new Process('sh /folder_name/file_name.sh');
to use the following syntax:
$process = new Process('/folder_name/file_name.sh');
The only problem with is that when uploading to a Linux server it will need to be changed to call sh.
Hope this helps anyone who hit this issue when following the accepted answer in Windows.
In Symfony 5.2.0 that used by Laravel 8.x (same as current Symfony Version 6.0 used by Laravel 9.x), you need to specify the sh command and your argument, in your case:
use Symfony\Component\Process\Process;
$process = new Process(['/usr/bin/sh', 'folder_name/file_name.sh']);
$process->run();
The system will find folder_name/file_name.sh from your /public folder (if it executed from a url), if you want to use another working directory, specify that in the second Process parameter.
$process = new Process(['/usr/bin/sh', 'folder_name/file_name.sh'], '/var/www/app');
And the /usr/bin/sh sometimes have a different place for each user, but that is a default one. Type whereis sh to find it out.

Unable to execute Python script from PHP shell_exec

I'm trying to execute a python (Python 3) script from the PHP shell_execfunction but it doesn't seem to be working, I'm trying it on a windows system but it will be in CentOS 7 in production.
In the below code snippet nothing happens when the PHP runs. But it I use the $response = shell_exec("php -v"); line it displays the PHP version ok. I've also tried running the python script directly on the command line and it runs fine with no errors.
Code
$command = "\"C:\Programs\Python\Python36-32\python.exe\" \"E:\\htdocs\\dev\\_nodes\\jobs\\jobCheck.py\" \"https:/www.google.ie\" 1";
$response = shell_exec($command);
//$response = shell_exec("php -v");
echo $response;
Command Line
"C:\Programs\Python\Python36-32\python.exe" "E:\\htdocs\\dev\\_nodes\\jobs\\jobCheck.py" "https:/www.google.ie" 1
Update #1
I've gotten the python script to run via PHP, but the python script crashes once it hits driver = webdriver.PhantomJS(), the import exists in the python script from selenium import webdriver and all this functionality works fine once called directly via the command line but doesn't seem to work once the python script is called from PHP.
Here's a sample script that prints out test 1 but not test 2 when called from the PHP script.
from selenium import webdriver
print("test 1")
driver = webdriver.PhantomJS()
print("test 2")
driver.quit()
Update #2
Looks like it was an issue with paths, there is a file created in the python script that was using a relative path, once I changed the path to be a full path the script ran ok both in the command line and when being called via the PHP script.
You need to scape the command string.
Try using escapeshellarg function.
$command = escapeshellarg('C:\Programs\Python\Python36-32\python.exe "E:\htdocs\dev\_nodes\jobs/jobCheck.py" "https:/www.google.ie" 1');
$response = shell_exec($command);
//$response = shell_exec("php -v");
echo $response;
http://php.net/manual/pt_BR/function.escapeshellarg.php

Launch Process comand from a controller - Symfony 2.1

I have a doubt launching a command process from my controller. The command process work fine from command line, and from my controller works using
MyProcess->run();
but not works
MyProcess->start()
I don't understand why!
I need launch it in background (asynchronously) and i would need use start()
//Launch process
$process = new Process('php ../app/console preinscripciones:enviar');
$process->start();//not work using start but it does it if I use run()
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new \RuntimeException($process->getErrorOutput());
}
echo $process->getOutput();
Thanks in advance!

Categories