Viewed   231 times

I'm trying to run a Python script from PHP using the following command:

exec('/usr/bin/python2.7 /srv/http/assets/py/switch.py arg1 arg2');

However, PHP simply doesn't produce any output. Error reporting is set to E_ALL and display_errors is on.

Here's what I've tried:

  • I used python2, /usr/bin/python2 and python2.7 instead of /usr/bin/python2.7
  • I also used a relative path instead of an absolute path which didn't change anything either.
  • I tried using the commands exec, shell_exec, system.

However, if I run

if (exec('echo TEST') == 'TEST')
{
    echo 'exec works!';
}

it works perfectly fine while shutdown now doesn't do anything.

PHP has the permissions to access and execute the file.

EDIT: Thanks to Alejandro, I was able to fix the problem. If you have the same problem, don't forget that your webserver probably/hopefully doesn't run as root. Try logging in as your webserver's user or a user with similar permissions and try to run the commands yourself.

 Answers

2

Tested on Ubuntu Server 10.04. I hope it helps you also on Arch Linux.

In PHP use shell_exec function:

Execute command via shell and return the complete output as a string.

It returns the output from the executed command or NULL if an error occurred or the command produces no output.

<?php 

$command = escapeshellcmd('/usr/custom/test.py');
$output = shell_exec($command);
echo $output;

?>

In Python file test.py, verify this text in first line: (see shebang explain):

#!/usr/bin/env python

Also Python file must have correct privileges (execution for user www-data / apache if PHP script runs in browser or curl) and/or must be "executable". Also all commands into .py file must have correct privileges:

Taken from php manual:

Just a quick reminder for those trying to use shell_exec on a unix-type platform and can't seem to get it to work. PHP executes as the web user on the system (generally www for Apache), so you need to make sure that the web user has rights to whatever files or directories that you are trying to use in the shell_exec command. Other wise, it won't appear to be doing anything.

To make executable a file on unix-type platforms:

chmod +x myscript.py
Saturday, August 6, 2022
5

I resolve the problem. I changed the system_exec command with exec, put the json object in quotes.

this is my code now:

$json = str_replace(""", "\"",  json_encode($jsonArray));
$script = "C:UsersmadalinaDesktopworkspacescript.py";
$blander_path = "C:Program FilesBlender FoundationBlender";
$output = exec("cd $blander_path && blender -b -P $script -- "$json"", $data);
print_r($data);
Wednesday, December 21, 2022
 
4

I just came up with this code that will run a process, and terminate it if it runs longer than $timeout seconds. If it terminates before the timeout, it will have the program output in $output and the exit status in $return_value.

I have tested it and it seems to work well. Hopefully you can adapt it to your needs.

<?php

$command = 'echo Hello; sleep 30'; // the command to execute
$timeout = 5; // terminate process if it goes longer than this time in seconds

$cwd = '/tmp';  // working directory of executing process
$env = null;    // environment variables to set, null to use same as PHP

$descriptorspec = array(
        0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
        1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
        2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
);

// start the process
$process    = proc_open($command, $descriptorspec, $pipes, $cwd, $env);
$startTime  = time();
$terminated = false;
$output     = '';

if (is_resource($process)) {
    // process was started
    // $pipes now looks like this:
    // 0 => writeable handle connected to child stdin
    // 1 => readable handle connected to child stdout
    // Any error output will be appended to /tmp/error-output.txt

    // loop infinitely until timeout, or process finishes
    for(;;) {
        usleep(100000); // dont consume too many resources

        $stat = proc_get_status($process); // get info on process

        if ($stat['running']) { // still running
            if (time() - $startTime > $timeout) { // check for timeout
                // close descriptors
                fclose($pipes[1]);
                fclose($pipes[0]);
                proc_terminate($process); // terminate process
                $return_value = proc_close($process); // get return value
                $terminated   = true;
                break;
            }
        } else {
            // process finished before timeout
            $output = stream_get_contents($pipes[1]); // get output of command
            // close descriptors
            fclose($pipes[1]);
            fclose($pipes[0]);

            proc_close($process); // close process
            $return_value = $stat['exitcode']; // set exit code
            break;
        }
    }

    if (!$terminated) {
        echo $output;
    }

    echo "command returned $return_valuen";
    if ($terminated) echo "Process was terminated due to long executionn";
} else {
    echo "Failed to start process!n";
}

References: proc_open(), proc_close(), proc_get_status(), proc_terminate()

Friday, November 4, 2022
5

Your web server likely runs with other privileges than yourself. Possible problems include:

  • Path/file permission: can the web server user access the files it needs?
  • Different environment: are all necessary environment variables (PATH, Python-specific stuff, …) set?
  • Configuration: are there per-user configurations for Python or the module?

Tip: execute set in both the command prompt and from the PHP process and check the differences.

Friday, September 9, 2022
2

Using compile

I've come up with a solution using the built-in function compile, as follows.

Contents of the file main.py:

with open('test.py') as f:
    source_code = f.read()
compiled = compile(
    source_code,
    filename='test.py', mode='exec', optimize=2)
exec(compiled)

Contents of the file test.py:

if __debug__:
    print('Debug ON')
else:
    print('Debug OFF')

The output from running python main.py is:

Debug OFF

Possible values for the parameter optimize:

  • -1: use same optimization level as the Python interpreter that is running the function compile
  • 0: no optimization, and __debug__ == true
  • 1: like -O, i.e., removes assert statements, and __debug__ == false
  • 2: like -OO, i.e., removes also docstrings.

Don't know if it's the best option, just sharing if can be useful fo others.

Using subprocess.run

The subprocess-based approach is still more concise, and can be made portable by using sys.executable:

import subprocess
import sys

if not sys.executable:
    raise RuntimeError(sys.executable)
proc = subprocess.run(
    [sys.executable, '-OO', 'test.py'],
    capture_output=True, text=True)
if proc.returncode != 0:
    raise RuntimeError(proc.returncode)

The above code calls the function subprocess.run.

The check for the value of the variable sys.executable is motivated by the documentation of CPython:

If Python is unable to retrieve the real path to its executable, sys.executable will be an empty string or None.

The check is implemented with a raise statement, instead of an assert statement, in order to check also in cases that the above Python code is itself run with optimization requested from Python, e.g., by using python -O or python -OO or the environment variable PYTHONOPTIMIZE.

When optimization is requested, assert statements are removed.

Using raise statements also enables raising an exception other than AssertionError, in this case RuntimeError.

For running Python code that is within a function inside the same source file (i.e., inside main.py, not inside test.py), the function inspect.getsource can be used, together with the option -c of python.

By the way better answers are welcome!

Monday, August 15, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :