PHP的exec():运行bash脚本来配置环境,然后执行Python程序 [英] PHP exec(): Running bash script to configure environment then executing Python program
问题描述
Shell是tcsh的。 PHP v5.1.6。在redhat 5.7。 safe_mode设置为OFF。
Shell is tcsh. PHP v5.1.6. Redhat 5.7. Safe_mode is OFF.
使用EXEC从浏览器中运行PHP脚本:
Running php script from the browser using exec to:
- 在bash脚本通过配置命令源环境
- 运行Python程序依赖于由bash脚本设置环境(节目输出到标准输出)
该工程在命令行($ SHELL = tcsh中):
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"
这没有。 Python程序返回,表明环境设置不正确(无法找到某些库等)错误:
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 );
?>
四,检查权限和一切看起来OK。
Quadruple-checked permissions and everything looks OK.
谢谢!
推荐答案
四件事情要注意。
1 - PHP不能有杠杆的safe_mode 执行exec()
1 - PHP must not have safe_mode on to leverage exec()
2 - shell脚本脚本需要有#/斌/庆典
将在文件的顶部声明,而不是传递到<$ C $! C>执行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 the exec()
3 - Python脚本必须在脚本的顶部有#的/ usr / bin中/ Python的
,而不是试图通过执行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 - 这是遍历所有目录找到脚本必须是可读
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.
这篇关于PHP的exec():运行bash脚本来配置环境,然后执行Python程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!