59319

PHP exec(): Running bash script to configure environment then executing Python program

Question:

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

Running php script from the browser using exec to:

<ol><li>configure environment via source command on bash script</li> <li>run a python program relying on the environment set up by the bash script (program outputs to STDOUT)</li> </ol>

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!

Answer1:

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.

Recommend

  • Using ASIHTTPRequest's Reachability
  • sending data to payment gateway and back - possible problems
  • Get GitHub username from commit history
  • Why does React.js' API warn against inserting raw HTML?
  • Shell environment variables in vagrant files are only passed on first up
  • Using `diff` from R via `system(..)`
  • Check for stdout or stderr
  • Running exec() command with dev>null & doesn't work, but dev>null does
  • python - terminate child process when script invoked from bash
  • How is user.dir configured for Tomcat servlets?
  • Error building MEX-files (MATLAB 2012a) on Kubuntu '/usr/bin/ld: cannot find -lstdc++'
  • Read stdin in chunks in Bash pipe
  • date: illegal option — d, Find difference between two dates
  • mCamera.setpreview{@override onPreviewFrame() } not work
  • How to run bash commands like “npm install” on complie
  • Calling java project from Mathematica
  • PHP file_exists() anomaly
  • Syntax error near unexpected token 'elif'
  • Insertion large number of Entities into SQL Server 2012 [duplicate]
  • Building jamvm 1.5.4 on OS X Lion
  • triggering user space with kernel
  • Add reference to ASP.NET 5 Class Library from Framework 4.5 Class Library Project
  • How can I get the full list of running processes on a Mac from a python app
  • During installation of Django, why do I keep getting ImportError: No module named django?
  • where do I find the xml.dom python package for the python-2.6.0-8.9.28 and I have a suse/x86_64 vers
  • Python pickle not one-to-one: different pickles give same object
  • bad substitution shell- trying to use variable as name of array
  • Redirect STDERR in OPEN pipe comand. Perl Linux
  • Get specific string
  • Differences in dis-assembled C code of GCC and Borland?
  • Algorithm for a smudge tool?
  • ILMerge & Keep Assembly Name
  • How to handle AllServersUnavailable Exception
  • Large data - storage and query
  • WOWZA + RTMP + HTML5 Playback?
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • Checking variable from a different class in C#
  • reshape alternating columns in less time and using less memory