5

Inside my automation.php controller, I have the following function:

public function deploy_test() {

      echo json_encode(system("python --version"));
 }

When the user wants to deploy a test, by clicking a test button in the webpage, he would be able to accomplish such a task. However, when I click the test button, my output is:

""

Meanwhile, when I execute the same function with the command:

public function deploy_test() {

    echo json_encode(system("ls -l"));
}

I'm getting:

total 32
drwxr-xr-x. 15 philippe philippe 4096 Mar  4 16:48 application
drwxrwxr-x.  2 philippe philippe 4096 Mar  4 17:28 css
-rw-r--r--.  1 philippe philippe 6357 Jan 30 11:53 index.php
drwxrwxr-x.  2 philippe philippe 4096 Feb 27 15:38 js
-rw-r--r--.  1 philippe philippe 2496 Jan 30 11:53 license.txt
drwxr-xr-x.  8 philippe philippe 4096 Jan 30 11:53 system
drwxr-xr-x. 12 philippe philippe 4096 Jan 30 11:53 user_guide

Could someone please help me to get that straighten out?

8
  • 1
    does the python command work when typed directly into the command line? Commented Mar 5, 2013 at 6:36
  • @Jeemusu It does, and it also works when I execute the command above from a simple test.php file. Commented Mar 5, 2013 at 17:06
  • Are the test.php and codeigniter code being run on the same server? Commented Mar 7, 2013 at 7:11
  • @Jeemusu Yes, they are Commented Mar 7, 2013 at 17:24
  • what does python -c "print(1); exit(10)" and env python --version do? Commented Mar 10, 2013 at 10:31

5 Answers 5

4
+25

The problem is not with your code or PHP.

The problem is with your permissions.

php uses permissions which are set in the env-vars of apache.

Which is ideally set as :

User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}

under your apache2 / httpd conf file.

For example:

Try running:

<?= `whoami` ?>

via your shell and via your browser.

Your browser will probably say www-data and shell will say your username or if you are on AWS - default, you would get root

You do not have to use system() , use exec() instead.

We should be close as :

echo json_encode(exec("python --version"));

Performing operations will require you to have correct User and Groups set.

Look up for : In the shell, what does " 2>&1 " mean?

So your code should be :

echo json_encode(exec("python --version 2>&1"));

Hope it helps!

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

Comments

1

This works fine for my production server

public function deploy_test() {
    echo json_encode(system("python --version 2>&1"));
}

with the output

Python 2.7.3
"Python 2.7.3"

Output of the unix command printed twice as system() itself outputs the result to browser. So exec() can be used in the place of system to avoid this.

public function deploy_test() {
    echo json_encode(exec("python --version 2>&1"));
}

which outputs

"Python 2.7.3"

as expected.

Comments

0

I suspect it is not in the path. Try:

Type the full path to the python command (such as /usr/bin/python --version)

  1. Find out with the command which, 'which python'

  2. try executing your script from the command line, 'php script.php' => sometimes the web server sets up things differently

  3. make sure the error displaying is enabled with

    ini_set('display_errors',1);

1 Comment

a couple of things: I created a mock test.php file, and I executed the program, and it worked as supposed. Then when I executed my script.py the results were the expected. The problem appears to be when I try to execute it from codeigniter.
0

system returns only last line. system

public function deploy_test() {

      system("python --version", $out);
      echo json_encode(implode($out));
 }

2 Comments

it returns 0 only. and $out needs to be an array in order to work with implode ( it is not ).
Sorry. Python -v returns output into second stream. You have to use $retval = exec("/usr/bin/python --version 2>&1", $out); var_dump($retval);
0

It's a bit of hack, but you can find out the version number with 1 decimal even if you aren't allowed to execute python (which is the case with the 'user' CI).

//find python path
exec("which python", $path);

//show all subdirs in python    
exec("ls -l ".$path[0]."*", $output);
$output = implode("\n", $output);

//preg match on version numbers    
preg_match_all("#python(\d+(?:\.\d{1,2})?)#", $output , $matches);
$installed_versions = $matches[1];

//sort in reversing order
$versions_sorted_desc = array_reverse($installed_versions);

//latest version is element 0
$latest_version = $versions_sorted_desc[0];

echo $latest_version;

Comments

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.