PHP shell_exec() script running from CPanel terminal but not from ajax POST call

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.

Rushil Joshi

Posted 2019-08-20T17:39:08.023

Reputation: 1

My first guess is that it's a PATH issue. Can you please edit your post to include the output of this test.php: <?php print shell_exec('echo $PATH'); ?>' Does it show a PATH at all? Is yourpython3.6in that path? If either answer is no, you'll need to specify the full path to/where/ever/you/have/python3.6`

– Jim L. – 2019-08-20T17:51:08.137

@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 to python3.6 in your PHP code. Possibly the full path to test.py also. – Jim L. – 2019-08-20T18:40:36.907

@JimL. Thank you! This worked. – Rushil Joshi – 2019-08-20T21:13:00.387

Answers

0

You are missing environment values when invoking python from the web server that are present in your shell. Candidates are PATH and the variables mentioned in the python man page.

Try this:

env -i python3.6 test.py

You will likely get some error messages that will hopefully point to the problem. Then try variables that are present in your shell environment, like:

env -i PYTHONHOME=$PYTHONHOME python3.6 test.py

Add all necessary environment variables. When this works, add them to the call to the script. When calling the script, you have supply the actual values for the variables, you can't refer to them as $PYTHONHOME.

RalfFriedl

Posted 2019-08-20T17:39:08.023

Reputation: 1 370

when I run "env -i python3.6 test.py", the terminal returns "python3.6: No such file or directory" – Rushil Joshi – 2019-08-20T18:29:48.790

Then one of the missing things is an entry in the PATH. Find out where it is (type python3.6) and add it to the PATH. – RalfFriedl – 2019-08-20T21:51:58.817