When my PHP script runs a subprocess (with any of the relevant functions such as exec()
, system()
, passthru()
, popen()
, proc_open()
, and maybe others I've forgotten) I can control the values of environment variables the process starts with (either by changing the script's environment itself, or, at least in the case of proc_open()
, by passing the desired environment as an argument). But is there a way for my script to get the state of the environment the process finishes with (that is the process potentially changes the environment, and I wish my script to be able to track those changes). Using getenv()
is useless as the script`s environment is not changed by the subprocess. Any ideas regarding whether this can be done, and how?

有没有办法在PHP中执行子进程结束时获取环境状态?
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- dongtang6775 2012-08-28 15:01关注
So, I don't like it, but this is what I've come up with: I've written a script that outputs the whole environment as a single line, and I'm calling it right after my invoked process, as part of the same command. After that I can analyse the output, extracting the environment from the last line, and discarding it in order to process the output from my original process.
There are many options to implement this. What I've done is along the lines of:
1) Creating a Python script, naming it, for example
printenv
, which looks like this:#!/usr/bin/python2.7 import os print os.environ
2) When invoking my process, instead of
<some command>
I invoke<some command> && printenv
.Of course, the script does not have to be in Python, and it does not have to output the environment in Python's
print
format. (But this format is rather convenient, and can be passed to a JavaScript assignment, for example.)本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报