I'm trying to run a java program and return the output then use that on my page.
if ($program) {
$output = shell_exec("java ftoa 12");
var_dump($output);
}
For some reason, that var_dump puts out null. I have ftoa.class in the same directory by the way. Also, the server my school is using runs an older version of php, not sure which one. Also when I run the command from the php command line, it works fine.
$:php -a
php>$output = shell_exec("java ftoa 12");
php>echo $output;
php>(desired output)
Related
Hi I am running a python script inside PHP using the shell_exec command,
However how would I know if the execution of the python has been completed already?
Here is portion of my code
$py_path = 'app/public/engines/validation/delta';
chdir($py_path);
$py_var = 'python scraped_data.py';
$exec = shell_exec($py_var);
How would I know if the python is finished its processing?
I wanted to know because I have a series of processes to run but it needs the first process to be completed first.
Thank You,
If you check php.net description of shell_exec() is:
shell_exec — Execute command via shell and return the complete output as a string
So you can edit your python script to output true or 1 once script is finished.
then you can simply code something like this:
$py_path = 'app/public/engines/validation/delta';
chdir($py_path);
$py_var = 'python scraped_data.py';
$exec = shell_exec($py_var);
If($exec){
//Execute another script or do what you wish
}
I am trying to run a script that saves a derived number using PHP. I've confirmed that this script works manually from the debian terminal when I execute this command, it saves a file with my number and echos
"file saved"
./number-code-gen "12345678-1234-123456"
I've confirmed that the script works to an extent by running it without parameters using this from PHP:
$output = exec("./number-code-gen", $output2);
print_r($output2);
It echos back:
"No Arguments. Please enter a number."
I've tried all of the following with and without quotes around the argument (works either way in terminal)
exec('./nfz_core "12345678-12345678-12345678"', $output2);
print_r($output2);
$output = shell_exec('./nfz_core "12345678-12345678-12345678"');
echo $output;
passthru('./number-code-gen 12345678-1234-123456',$rtn);
echo $rtn;
I don't get echos or errors for either of these, and my file isn't generated. Is there a specific thing I'm missing here?
Basically, I just want to run ./number-code-gen 12345678-1234-123456 from PHP, but it isn't working. I am running Debian and PHP version: 5.6.33
Here's the issue:
I am using R to run some statistical analysis. The results of which will eventually be sent to a an embedded swf on the user's client machine.
To do this, I have PHP execute a shell script to run the R program, and I want to retrieve the results of that program so I can parse them in PHP and respond with the appropriate data.
So, it's simply:
$output = shell_exec("R CMD BATCH /home/bitnami/r_script.R");
echo $output;
But, I receive nothing of course, because R CMD BATCH writes to a file. I've tried redirecting the output in a manner similar to this question which changes my script to
$output = shell_exec('R CMD BATCH /home/bitnami/raschPL.R /dev/tty');
echo $output;
But what I get on the console is a huge spillout of the source code, and nothing is echoed.
I've also tried this question's solution in my R script.
tl;dr; I need to retrieve the results of an R script in PHP.
Cheers!
If it writes to file perhaps you could use file_get_contents to read it?
http://php.net/manual/en/function.file-get-contents.php
Found it, the answer is through Rscript. Rscript should be included in the latest install of R.
Using my code as an example, I would enter this at the very top of r_script.R
#!/usr/bin/Rscript --options-you-need
This should be the path to your Rscript executable. This can be found easily by typing
which Rscript
in the terminal. Where I have --options-you-need, place the options you would normally have when doing the CMD BATCH, such as --slave to remove extraneous output.
You should now be able to run your script like so:
./r_script.R arg1 arg2
Important! If you get the error
Error in `contrasts<-`(`*tmp*`, value = "contr.treatment") :
could not find function "is"
You need to include the "methods" package, like so:
require("methods");
Perhaps,a much simpler workaround, would be:
$output = shell_exec('R CMD BATCH /home/bitnami/raschPL.R > /dev/tty 2>&1');
echo $output;
Redirects both STDOUT and STDERR, since R outputs to STDERR, by default.
I have a C program which has been compiled to an executable. I can run this program from my console. I am trying to get the output from this program through my web browser so I am using the exec command in PHP. My PHP script works fine when I execute it from the command line, but when I call it over a browser I get no input. Here is my PHP program
<?php
echo exec('/var/www/html/./readcard');
?>
The readcard program has 777 permissions. I am guess the issue is something to do with permissions?
You aren't capturing the output. The second argument to exec consists of an array to which the output lines will be placed.
<?php
$output=array();
$rv=0;
exec('/var/www/html/./readcard',$output,$rv);
if($rv!=0)
{
die("readcard failed, got a return value of $rv\n");
}
foreach($output as $line)
{
echo("<p>$line</p>\n");
}
?>
You probably just echo the return code of the script, which is zero. You can either redirect the output to a file and then serve that from php, or pipe the output stream directly back to php code.
Try
<?php
$output = array();
exec('/var/www/html/./readcard', &$output);
?>
I am trying to run a simple PHP that runs a powershell script,
if I use this code I get the results in a command window, but I get an empty array in the browser:
<?php
exec("powershell C:\\Inetpub\\wwwroot\\my_shell.ps1 < NUL", $output);
echo "<pre>";
print_r($output);
echo "</pre>";
?>
I believe the NUL will discard the output, however it works in the browser found it on [this Fourm][1]
If I use this code, without the NUL I will get the results on a command window but if I run the script in the browser will keep loading forever and it will never give me any results:
exec("powershell C:\\Inetpub\\wwwroot\\emsrDev\\manual_shell.ps1", $output);
Same results if I do it this way:
$output = shell_exec("powershell C:\\Inetpub\\wwwroot\\emsrDev\\manual_shell.ps1");
The powershell Script runs fine if I ran it independently:
$cmd = "cmd.exe";
&$cmd "/C echo update tasks set last='manual' where id='8'; | sqlplus vvv/www#xxx";
So I need to execute this in a browser and get the output.
The powershell process isn't terminating, which causes the browser to hang. Add the following to the end of your script
Stop-Process -processname powershell*