running process in background on hangs PHP page
I did as explained in this above link but it did not work.
Question:
I want to send mail using Python from PHP. When I call Python, my page waits for a response from Python. I want to run Python in the background.
I also redirect Python's response to another location ex: "E:\\nil.txt", but it still waits for a response.
I am working on Windows 7. My problem is that all the answers I could find were for Linux.
My PHP code:
public static function callPython($scriptName, $data){
$path = "C:\\Users\\Aditya\\Desktop\\".$scriptName.".py";
//$data=array("to"=>$to,"name"=>$name,"headingSection"=>$headingSection,"body"=>$body,"subject"=>$subject);
$jsonArray = json_encode($data);
$jsonArray = str_replace('"', '\"', $jsonArray);
$dir= "E:\\nil.txt";
$cmd = 'python '.$path.' '.$jsonArray.' >'.$dir.' 2>&1 &';
var_dump($cmd);
shell_exec($cmd);
}
If you need my Python script, then I will post it here.
the main thing is how the python file is getting executed from the php code you have written? it is only loading the file in the $path file. and how are you getting the $data while it is not having anything as you are not storing any.
hope you get my point.
i will give you an idea.
When the php file is calling the python file. the python file should execute like we do in cmd. python some.py like this. then whatever is returned should be loaded in your php function to see it.
hope it helps
Related
I have a python script which is needed to be run by php:
<?php
$command = escapeshellcmd('/home/Desktop/test.py');
$output = shell_exec($command);
echo $output;
?>
The out put of the python script is a binary file but I get the following error in the log:
Unable to access the X Display, is $DISPLAY set properly?
The php code works fine from the terminal but no luck when I try to run it from the browser. Any idea what is going on? Ideally, I don't want to change my program. I want to know how you can rectify the X Display error. Is there a way to check if $DISPLAY is set properly? ( I am working with Ubuntu)
I tried this : pidof X && echo "yup X server is running" on my terminal and it is saying yup x server is running!
Add the following text as the first line of your Python script:
#!/usr/bin/python
Without this, the kernel doesn't know what interpreter to run your script with, and may end up trying to launch it using /usr/bin/import (because that word probably appears on the first line of the script). The import utility requires access to the X11 display (it's a screenshot utility, basically), which is why you're getting this error.
The python file you need may need to open a window to run. You say you saw it run in terminal though? What's test.py? Is it propitiatory?
If you try using this as a command in your PHP: (not 100% that the shell escape won't strip this so may need to comment that out)
python -c \'print \"Test\"\'
and see if you get the output text back. If so it's a python issue not PHP and the test.py file may be instantiating something that needs the $DISPLAY var set. PHP doesn't set the $DISPLAY var as it is shell commands not GUI.
try popen
$command = "/usr/bin/python /home/Desktop/test.py";
$handle = popen($command, "r");
"r" for read
$read = fread($handle, 10);
10 is the size of output you want to take,
echo $read ;
hope it helps,
I am trying to run a perl script from php that requires parameters to be passed to the perl script to run correctly. The following is the correct usage of the perl script from the linux terminal:
/home/user/test.pl -a alpha -b beta
or just
/home/user/test.pl -a alpha
I have execute permissions on the script and can run it without any parameters and the correct usage from the script is displayed back to my browser.
Below is the PHP code that works by displaying the usage back to my browser:
$result = shell_exec('/home/user/test.pl');
echo $result;
And the following is the problem code which I can not for the life of me figure out:
$test = $_POST['test'];
$result = shell_exec('/home/user/test.pl -a'.' '.$test);
echo $result;
Can anyone tell me what it is that I am missing to make this work correctly?
Thank you for the help.
My issue resided within the perl script itself and a specific line that was trying to output to a log file which the apache user did not have access to. I was calling the script correctly the whole time but once I was able to get to the server side logs (granted by system admin) I saw the issue was buried within the Perl script and not in php.
i'm using an API, which creates a JSON file when the python script is executed, so i've tried to use exec to run the python script and thne retrieve the json. However the python script does not seem to execute. What am i doing wrong? i'm trying it on a apache using MAMP
exec('python http://localhost:8888/examples/recent_matches_to_json.py');
$json = file_get_contents('http://localhost:8888/examples/recent_matches.json');
$obj = json_decode($json);
var_dump($obj->recent, true);
Uh... You should download the script and save it, then run the interpreter on it.
Something like:
$py_script = file_get_contents('http://localhost:8888/examples/recent_matches_to_json.py');
file_put_contents('recent_matches_to_json.py', $py_script);
exec('python recent_matches_to_json.py');
If you intend to download the script from the local computer, why not run it directly? Like exec('python /home/peter/site/examples/recent_matches_to_json.py').
You could also set up a web/WSGI server in that Python script so you could run it directly by just sending a HTTP request (like http://localhost:9999/recent_matches_to_json/).
I have created a small php script locally that runs a java application in command line. This java application continuously runs and never finishes. As it runs, it outputs command line text. Here is the code:
<?php
set_time_limit(0);
$command = "java -Xms124M -Xmx124M -jar myapp.jar";
$end = " 2>&1";
$in = $command . $end;
$out = exec($in);
var_dump($out);
?>
My problem is that the output is never printed because the app never stops running. Is there a way to get the php to print out each line that is returned as the app is running?
Hopefully I am making sense here (Let me know if I am not).
You might want to take a look at the passthru function and the popen function. These should return output as it occurs (although passthru might buffer the output).
One solution would be to launch your process as a background process and redirect output to a file. You could then read the output file in PHP, but you really shouldn't leave PHP running like that, especially since your java process is expected to never end. A better solution would be to use AJAX polling to have PHP return any recent updates to the output file every few seconds or something.
I am trying to invoke a script which takes several seconds (web services with 3rd party) using the PHP exec call. After much struggling, I reduced this to the classic hello world example. The calling script looks like:
exec('/usr/bin/php /home/quote2bi/tmp/helloworld.php > /tmp/execoutput.txt 2>&1 &');
When I run this, the output execoutput.txt contains a copy of the invoking script page, not hello world as I expected.
Why can't I get this PHP script to execute using exec? Note that when I change the command to something like ls -l, the output is a directory listing as expected. btw, in case it matters, I did chmod the called script to 755...
Update - I moved the exec call to the end of the calling script and at least now I don't see the calling script executed in the output. Thx to posters and I will try some of these ideas.
Help!
Thanks
Steve
I had this issue also and it turns out this is a bug in php (#11430). The fix is to use php-cli when calling another php script within a php script. So you can still use exec but rather than use php use php-cli when calling it in the browser:
exec("php-cli somescript.php");
This worked for me.
What exec is doing is taking the rightmost command and appending it to your destination. If you have the shebang line in your php script, you shouldn't need to include the binary directive of the php interpreter.
if you just want the script's output, try:
exec('/home/quote2bi/tmp/helloworld.php > /tmp/execoutput.txt 2>&1 &')
however if you do not want the errors to be in the file, you should redirect the STDERR prior to outputting to the file. Like so:
exec('/home/quote2bi/tmp/helloworld.php 2> /dev/null > /tmp/execoutput.txt')
the above should only output the "Hello World" to the execoutput.
Edit:
Interesting you are getting this behaviour. You stated the command "ls" worked. Try making an alias for this and forward it to a file like so:
alias pexec='php /home/quote2bi/tmp/helloworld.php'
then
exec('pexec > /tmp/execoutput.txt 2>&1 &')
it seems to be a problem with the way exec handles input as opposed to the shell itself.
-John
The problem is with PHP itself, it treats everything as $argv in the script. It doesn´t redirect the output to a file ou to /dev/null.
I faced the same problem some time ago. What I did is to create a runscript.php in /opt/php-bin and then inside this script run what It should be running. Something like this:
$script = $argv[1]
$params = implode(' ', array_slice($argv, 2));
$cmd = "{$script} {$params} > /dev/null &";
$output = array();
$return = 0;
exec("php {$cmd}", $output, $return);
exit((int)$return);
And then you call it using:
exec('/opt/php-bin/runscript.php /path/to/your/script.php arg1 arg2')
It´s the only way I managed to get this working.
To avoid the stated problems of PHP in this area, why not put this in inside a shell script? PHP can then execute the shell script which has all the redirections handled internally.
If you need to dynamically change things, then why not write the shell script and then execute it (and of course, clean up afterwards)?
if you are just simply running a php script one possible way to execute the entire code is to use the include() that will run the php file and output any results. You cannot direct the output to a text file but it should appear in the browser window if you're Hello World php script looks like
<?php echo "Hello World!"; ?>
then it will spit that out in the browser. So your second code would look like
<?php include("helloWorld.php"); echo " PHP ROCKS";?>
resulting in a page that would look like,
Hello world! PHP ROCKS
This runs as if you run the script from browser.
This came across while working on a project on linux platform.
exec('wget http://<url to the php script>)
Hope this helps!!