3

I would like to run a few python scripts(I am aware of the risks , but I HAVE to get it done)

i tried using :

echo exec('python --version ');

as well as echo shell_exec('python --version ');

Also tried '/usr/bin/python ' instead of just python but I dont get any output at all. I have even added the www-data to the sudoers list, still not working.

What should I do ?

Running debian and python 2.7

5
  • Instead of python specify the path like /usr/local/bin/python Commented Mar 27, 2015 at 11:57
  • You can check where exactly is Python installed by running which python and then use this path in your code. Commented Mar 27, 2015 at 11:59
  • nop, did not work. any more ideas ? give me any thing that could be the problem Commented Mar 27, 2015 at 11:59
  • tried which python, giving /usr/bin/python. i have tried running it from that path Commented Mar 27, 2015 at 12:00
  • seems like there are some security measures involved, for example safe mode php.net/manual/en/features.safe-mode.php or something like suhoshin or apparmor Commented Mar 27, 2015 at 12:35

4 Answers 4

5

It seems python --version prints version info to stderr instead of stdout for some reason, so you'll need to redirect former to latter:

exec('python --version 2>&1');

Also, note that exec's return value is just the last line of the executed command's output. If you want to catch full output from command that returns multiple lines, you'll need to provide an array as exec's second argument:

$output = array();
exec($some_command, $output);
Sign up to request clarification or add additional context in comments.

1 Comment

can you please write an example ?
2

To capture the output of python --version use the following:

exec('python --version 2>&1', $output);
var_dump($output);

python --version outputs to stderr, which is a bug. It has been fixed in Python 3.4.0 (see changelog, issue #18338).

Comments

1

The reason you can't run python script from PHP is that, the system call requires to be root.Even if you make a sudo call, it requires password.So what you can do is add this line to the end of file : /etc/sudoers

www-data ALL=(ALL) NOPASSWD:ALL

In this way, any sudo call from the PHP will not require a password. Go ahead and execute the python script for an eg. exec("sudo python /home/pi/test.py"); This works for me.Let me know if it works.

Comments

0

When you type python on your Command-Line, your Shell is searching all folders which are configured in the Environment-Variable $PATH. Your PHP-Interpreter has a different Environment and therefore maybe does not find the python-Binary.

Try to obtain the complete Path to your python-Binary by running which python in your Shell and try to use this in PHP.

1 Comment

ive tried that , as already mentioned in the comment. any other ideas ?

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.