Running console program in PHP - php

I wrote a simple PHP code to execute a console program:
<?php
$cmd = escapeshellcmd('progName.exe arg1 arg2 arg3');
exec($cmd);
?>
If I run the command on the console directly on the server, it works. However, when I run the PHP on the browser, it doesn't work. The process progName.exe is running (checked using Task Manager on the server), but it never finishes. This program is supposed to compute some parameters from the arguments and write the result to a binary file, and also produce a .WAV file. Here is the error message I get on the browser:
Error Summary
HTTP Error 500.0 - Internal Server Error
C:\php\php-cgi.exe - The FastCGI process exceeded configured activity timeout
Detailed Error Information
Module FastCgiModule
Notification ExecuteRequestHandler
Handler PHP
Error Code 0x80070102
Then I wrote a simple console program that write a sentence to a text file (writeTxt.exe hello.txt). Using the same PHP script, I ran it on the browser and it works.
I already tried to increase the timeout on the server, but still have the same error.
What could cause this problem?

When you execute a program in PHP using the exec function (e.g. exec('dir')), PHP waits until it is ended or you sent it to the background and PHP comes back directly (see documentation, especially the comments).
According to your posted PHP sources ($cmd = escapeshellcmd('progName.exe arg1 arg2 arg3');) the program is not sent to background by PHP - so what stays is that progName.exe...
...sends itself or a fork to the background (unlikely, but look into the sources of progName.exe)
...is waiting for input (<-- this is my favorite)
I missed something ;-)
As I said I bet it is the second option. Hope that helped a bit.

Related

PHP runs Perl script from command line successfully but I get no output displayed

I have a problem displaying the results of a Perl script that I am calling from my PHP webpage. The Perl script constantly monitors a socket and will display the output of this when run from the command line and also saves the output to a file. I know the Perl script is being called and running successfully as the text file is being updated but I do not get the output on the webpage as I was hoping for.
I have tried using the system(), exec(), passthru() and they all allow the Perl script to run but still with no output on the webpage so I am obviously missing something. Am I using the correct functions? Is there a parameter that I need to add to one of the above to push the output back to the webpage that calls the Perl script?
One example of what I have tried from the PHP manual pages:
<?php
exec('perl sql.pl', $output, $retval);
echo "Returned with status $retval and output:\n";
print_r($output);
?>
Edited to include output example as text instead of image as requested.
# perl sql.pl
Connecting to the PBX 192.168.99.200 on port 1752
04/07 10:04:50 4788 4788 3256739 T912 200 2004788 A2003827 A
I'm no PHP expert, but I guess that exec waits for the external program to finish executing before populating the $output and $return variables and returning.
You say that your sql.pl program "constantly monitors a socket". That sounds like it doesn't actually exit until the user closes it (perhaps with a Ctrl-C or a Ctrl-Z). So, presumably, your PHP code sits there waiting for your Perl program to exit - but it never does.
So I think there are a few approaches I'd investigate.
Does sql.pl have a command-line option that tells it to run once and then quit?
Does PHP have a way to send a Ctrl-C or Ctrl-Z to sql.pl a second or so after you've started it?
Does PHP have a way to deal with external programs that never end? Can you open a pipe to the external process and read output from it a line at a time?

PHP hosted on IIS gives 500 on every alternate call

I have a PHP Script in which it executes a batch(.bat) file using passthru() function. The output of batch file is printed via using echo statement.
This PHP Script works absolutely fine when hosted on Apache webserver, however the same PHP script produces 500.0 error on every alternate call, when hosted on IIS 7.5
I did some research and found out that if PHP script takes long time to execute, the browser gets unresponsive.
Hence, I edited the PHP script to write into a file like "Before executing batch file" and "After executing batch file".
As there 500.0 error was displayed, the file was still getting updated by above lines. This concludes that while the script is getting executed but browser is displaying 500.0
Is there any settings that can be tweaked in IIS?
This problem occurs only for IIS 7.5. When I use Apache it works like a charm.
I've had the exact same problem as you; executing a batch file via exec(), shell_exec(), etc, would result in an internal 500 server error every other time I refreshed the page.
I resolved this by removing all PAUSE commands from the batch file.
Make sure you don't have any breaks in the flow of the batch file. That is, if user input is required at any point during the execution of the batch script php will hang and the server will time out.
Hope this helps!
(I'd comment but I don't have 50 reputation)

CentOS Apache/PHP exec function does not finish executing external program

I have tried and failed to understand why my command line program does not work in Apache environment using a PHP exec() function call. Here is the scenario:
Installed Apache 2.x and PHP 5.3.3 on CentOS 32-bit
Hard-coded a php file called myScript.php that contains a simple call to exec() such as:
exec("./imageManipuator testImage.jpg 512 512 >& output.log &");
The exec() function should call my program with the given parameters to process the request, redirect output to a file "output.log" all in a background process defined by '&'.
I check the log and the program executes 1/4 way through the program and terminates without a clue why.
I tried executing the PHP file via PHP Interactive shell call via:
$ php myScript.php
The execution of the program completely finishes!!! That brings me to feel:
What is wrong with my Apache/PHP configuration to disallow my program to execute all the way.
Perhaps a permission issue? I tried changing the logged user as the Administrator user but it did not change anything.
Perhaps a memory issue? Shouldn't there be a warning or notification if a memory limit has been reached? I have not verified this is the issue?
That is my issue. I know I am using the function exec() correctly to execute the program in the background because the same script works when calling the same file via PHP Interactive shell. What could be wrong in the Apache/PHP configuration that disallows my program to fully execute?
Any suggestions is a bonus as I have exhausted my ideas.

curl not executing properly when invoked by php

So I want to execute a bash command from PHP on my web server. I can do this using shell_exec. However, one of the commands I want to execute is curl. I use it to send a .wav file to another server and record its response. But when invoked from PHP, curl doesn't work.
I reduced the error to the following small example. I have a script named php_script.php which contains:
<?php
$ver=shell_exec("curl -F file=#uploads/2013-7-24-17-31-43-29097-flash.wav http://otherserver");
echo $ver
The curious thing is that when I run this php script from command line using php php_script.php, the result I get is
Status: 500 Internal Server Error
Content-type: text/html
However, if I run curl -F file=#uploads/2013-7-24-17-31-43-29097-flash.wav http://otherserver directly, I get the response I was expecting:
verdict = authentic
(Edit:) I should probably mention that if I put some bash code inside the shell_exec argument which does not contain curl, the bash command executes fine. For example, changing the line to $ver = shell_exec("echo hello > world"); puts the word "hello" into the file "world" (provided it exists and is writable). (End edit.)
Something is blocking the execution of curl when it is invoked from PHP. I thought this might be PHP's running in safe mode, but I found no indication of this in php.ini. (Is there a way to test this to make 100% sure?) What's blocking curl and, more importantly, how can I bypass or disable this block?
(And yes, I realize PHP has a curl library. However, I prefer to use commands I can run from the command line as well, for debugging purposes.)
cheers,
Alan
The reason is the administrative privileges when you run the command directly you are running it as root and thus the command gets executed. But, when you run the command through PHP it runs as an user. By, default user has not the privileges to run the shell_exec commands.
You have to change the settings of shell_exec through CPanel/Apache config file. But, it is not recommended to provide the shell_exec access to the user as it help hackers to attack on server and thus, proper care should be taken.
It would be more appropriate to use the curl library provided in PHP.

PHP not executing python script correctly

I'm executing a python script from a php page like so: exec("python ./test.py");
This script runs fine if I don't open a serial port in it. If I do, however, (and this is the whole point of calling the python script in the first place), the script doesn't execute properly.
If I call a simple python script that prints a statement -
print "This works!"
Then I get the desired output in my php page.
But, if I open a serial port, I no longer get the output of "This works!", and the serial data is not getting sent to the receiving device -
import serial
ser = serial.Serial("/dev/ttyACM0",9600)
print "This works!"
Both scripts run fine from the command line.
Is this a php limitation? I have tried other methods of execution such as popen and system, but they didn't work for me either.
Perhaps you aren't getting complete error reporting from your Python execution. Try adding raise Exception('Boo!') as the first line of you Python program to find out if you are or not. If you don't get the exception and a traceback, then your program is probably failing on the serial.Serial line, but you aren't hearing about it.

Categories