0
On my website, I have a javascript function that has a POST ajax call to a PHP file on the server (a GoDaddy VPS running CentOS 6), which executes a python script via shell_exec() and returns the output.
Javascript function:
function submitdivsheet()
{
$.post("test.php", {}, function(data,status){
console.log(data);
});
}
PHP script: test.php
<?php
$command = escapeshellcmd('python3.6 test.py');
$output = shell_exec($command);
echo $output;
?>
Python script: test.py
import sys
print("This is the output")
When I try to run the php file from the bash terminal on CPanel, there are no issues, as shown here. However, upon running the javascript function, only an empty string is output to the console. I'm not sure why it would only run on the CPanel terminal, but I suspect it could be a permissions issue somewhere. How can I fix this issue?
NOTE: This works fine on both the server and from the POST call if shell_exec() is not present. It seems that shell_exec is causing the problem.
EDIT: Upon changing the PHP script to:
<?php print shell_exec('echo $PATH'); ?>
This is the output.
My first guess is that it's a PATH issue. Can you please edit your post to include the output of this
– Jim L. – 2019-08-20T17:51:08.137test.php
:<?php print shell_exec('echo $PATH'); ?>' Does it show a PATH at all? Is your
python3.6in that path? If either answer is no, you'll need to specify the full path to
/where/ever/you/have/python3.6`@JimL. added! It shows a path, but python3.6 is not in that path. – Rushil Joshi – 2019-08-20T18:04:11.637
As my previous comment says then, you'll need to update your
test.php
file to include the full path topython3.6
in your PHP code. Possibly the full path totest.py
also. – Jim L. – 2019-08-20T18:40:36.907@JimL. Thank you! This worked. – Rushil Joshi – 2019-08-20T21:13:00.387