Shell is tcsh. PHP v5.1.6. Redhat 5.7. Safe_mode is OFF.
Running php script from the browser using exec to:
- configure environment via source command on bash script
- 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!
Four things to note.
1 - PHP must not have safe_mode on to leverage
exec()
2 - The shell script script needs to have
#!/bin/bash
to be declared at the top of the file rather than being passed into theexec()
3 - The python script must have
#!/usr/bin/python
at the top of the script rather than attempting to execute it through theexec()
statement.4 - All directories that are traversed to get to the script must be readable.
So the final should look like:
This should resolve all your issues.