从PHP CLI脚本运行命令之前导出shell环境变量 [英] export shell environment variable before running command from PHP CLI script
问题描述
我有一个脚本,使用 passthru()来运行命令。我需要在运行此命令之前设置一些shell环境变量,否则将无法找到它的库。
我尝试过以下操作:
putenv(LD_LIBRARY_PATH = / path / to / lib);
passthru($ cmd);
使用 putenv()似乎不传播到我正在运行的命令。它不能说它找不到它的库。当我在bash中运行 export LD_LIBRARY_PATH = / path / to / lib
时,可以正常运行。
以下(无效):
exec(export LD_LIBRARY_PATH = / path / to / lib);
passthru($ cmd);
如何从PHP设置一个shell变量,传播到PHP脚本的子进程? / p>
我只限于检查当前环境中是否不存在变量,并要求用户手动设置?
我不是很熟悉PHP的exec如何工作,但是你尝试过: exec(LD_LIBRARY_PATH = / path / to / lib $ cmd )
我知道这在大多数shell中都有效,但我不知道PHP在做什么。
编辑:假设这是正常的,要处理多个变量,只需将它们分开一个空格:
exec VAR1 = val1 VAR2 = val2 LD_LIBRARY_PATH = / path / to / lib $ cmd)
I have a script that uses passthru() to run a command. I need to set some shell environment variables before running this command, otherwise it will fail to find it's libraries.
I've tried the following:
putenv("LD_LIBRARY_PATH=/path/to/lib");
passthru($cmd);
Using putenv() doesn't appear to propagate to the command I'm running. It fails saying it can't find it libraries. When I run export LD_LIBRARY_PATH=/path/to/lib
in bash, it works fine.
I also tried the following (in vain):
exec("export LD_LIBRARY_PATH=/path/to/lib");
passthru($cmd);
How can I set a shell variable from PHP, that propagates to child processes of my PHP script?
Am I limited to checking if a variable does not exist in the current environment and asking the user to manually set it?
I'm not 100% familiar with how PHP's exec works, but have you tried: exec("LD_LIBRARY_PATH=/path/to/lib $cmd")
I know that this works in most shells but I'm not sure how PHP doing things.
EDIT: Assuming this is working, to deal with multiple variables just separate them by a space:
exec("VAR1=val1 VAR2=val2 LD_LIBRARY_PATH=/path/to/lib $cmd")
这篇关于从PHP CLI脚本运行命令之前导出shell环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!