0

I currently have a php page that my webserver serves. In order to display all the information I need to display on the page I need output from an external python script. So I have been using the exec() command of php to execute the python script and capture the output in an array of strings as follows:

$somequery = $_GET['query'];
$result = exec("python /var/www/html/query/myscript.py ".somequery."");

//some for loop to loop through entries in result and echo them.

However there are never any entries to be printed, yet when I run the command directly on the console of the server it will output correctly. I've tried echoing out the command on the webpage that I am executing and it's the correct command. The only thing I think it can be is that exec() doesn't stop the rest of the php program from executing before it finishes, leading to the loop i have printing out entries finding that $result is empty.

How can I ensure that exec() finishes executing before the rest of my php script? Are there maybe settings in php.ini that I would need to change? I'm not entirely sure.

EDIT: I've tried running and storing the output of shell_exec("echo hello"); and printing that output, it now prints. However, when running my command that takes a few seconds longer, the program never finishes executing it before going to the next line.

EDIT 2: I found my solution in the following post https://stackoverflow.com/a/6769624 My issue was with with the numpy python package I was using and I simply needed to comment out the line in /usr/lib64/python2.7/ctypes/init.py like the poster did and my script output correctly.

12
  • 1
    This has already been asked Commented Dec 16, 2012 at 1:09
  • 4
    FYI, that's completely insecure. Anyone can run anything that your web server user has access to. Be very careful with exec(). Commented Dec 16, 2012 at 1:10
  • It may have been asked, but none of those answers answer my question. I also am aware this insecure, this isn't for a production environment however.. just a proof of concept. Commented Dec 16, 2012 at 1:16
  • php exec always wait the finish Commented Dec 16, 2012 at 1:31
  • i think you need to use full path to python binary like /usr/bin/python Commented Dec 16, 2012 at 1:32

1 Answer 1

2

The correct way to get your shell output is like this:

exec("python /var/www/html/query/myscript.py ".somequery."", $result);

var_dump($result); //output should be in here

Give it a try.

Sign up to request clarification or add additional context in comments.

2 Comments

On a side note: putting the $_GET value directly into your exec() command without any sanitization is VERY dangerous. Easy example, if someone inserts $_GET['query'] as ` && rm -rf /` (DO NOT attempt to try this), you just got your whole server deleted. Be sure to run it through some kind of sanitization.
php.net/manual/en/function.escapeshellarg.php I use this when I'm actually running it on an accessible server. Also, I did a var_dump after running it your way and I simply get array(0) {}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.