1

I've got a simple PHP script that is using shell_exec to call a python script which should return the result "ok"

I can't seem to get this working - nothing is returned via the php file. I can run a python script from the CLI and it works, and also run shell_exec to rn other functions and that works. So its something specific to python which I can't work out.

test.py:

 #!/usr/bin/python3

import sys

id = sys.argv[1:]
#Do some stuff wih id

#Return success
sys.exit("ok")

index.php

<?php

$command = escapeshellcmd('/root/bin/tracker/test.py');
$output = shell_exec($command);
echo $output;

//This works
//$output = shell_exec("cat /etc/os-release");
//echo $output;
?>

Running /root/bin/tracker/test.py returns:

ok

I've tried setting ownership of test.py to www:www-run and also checked +x permissions.

This is running on SLES 15.1 PHP 7.2.5 Apache 2.4.33

4
  • Try adding the path to the python executable as well. Something like: /path/to/python /root/bin/tracker/test.py. Commented Mar 7, 2021 at 10:44
  • Still not luck. Tried adding: $output = shell_exec("/usr/bin/python3 /root/bin/tracker/test.py"); Commented Mar 7, 2021 at 10:45
  • sys.exit("ok") indicates an error, according to the docs shell_exec returns null when a error occurs. Use sys.exit(0) for success. Commented Mar 7, 2021 at 11:02
  • Thanks just for testing. Looks like some permissions issue as when I run php index.php it works, but not from the browser Commented Mar 7, 2021 at 11:03

1 Answer 1

3

Found the issue. Adding 2>&1 to the shell_exec clearly shows the error.

$output = shell_exec("/usr/bin/python3 /root/bin/tracker/test.py 2>&1");

[Errno 13] Permission denied

I moved the test.py file out of the root folder and into /srv/www/htdocs and its working fine now.

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

1 Comment

I think you should accept your answer if it fixes your problem

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.