0

Shell is tcsh. PHP v5.1.6. Redhat 5.7. Safe_mode is OFF.

Running php script from the browser using exec to:

  1. configure environment via source command on bash script
  2. run a python program relying on the environment set up by the bash script (program outputs to STDOUT)

This works from the command line ($shell = tcsh):

/bin/bash -c "source /path-to-config-bash-script/config.sh; /bin/path-to-python /path-to-python-program/prog.py 2>&1"

This does not. Python program returns an error indicating that the environment is not set up correctly (can't find certain libraries, etc.):

<?php
....
$cmd = "/bin/bash -c \"source /path-to-config-bash-script/config.sh; /bin/path-to-python /path-to-python-program/prog.py 2>&1\"";
$ret_val = exec( $cmd, $ret_arr, $err );
?>

Quadruple-checked permissions and everything looks OK.

Thanks!

1 Answer 1

1

Four things to note.

1 - PHP must not have safe_mode on to leverage exec()

2 - The shell script script needs to have #!/bin/bashto be declared at the top of the file rather than being passed into the exec()

3 - The python script must have #!/usr/bin/python at the top of the script rather than attempting to execute it through the exec() statement.

4 - All directories that are traversed to get to the script must be readable.

So the final should look like:

$cmd = "/path-to-config-bash-script/config.sh; /path/to-python/program/prog.py 2>&1";

This should resolve all your issues.

3
  • Thanks but your suggestions won't work for a number of reasons. Commented Nov 14, 2013 at 0:43
  • Sorry, I ran over the 5 minute editing limit...First, the shell script must be "sourced," as the exports therein need to persist so that they can be used by the python program invocation that follows after. Secondly, the default shell is tcsh so the entire command string needs to be executed within a bash invocation. "./" and "source" are not the same when the script executes as a shell other than the current running shell (especially with "export" commands). Commented Nov 14, 2013 at 1:08
  • exec("/bin/bash -c 'source /path-to-config-bash-script/config.sh; /bin/path-to-python /path-to-python-program/prog.py 2>&1'", $out_arr, $err_arr); Works when running it from command-line php. So at least I have that going for me...The source part still fails when invoked via apache however. Commented Nov 15, 2013 at 0:32

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.