douqu2481 2012-08-23 11:45
浏览 46
已采纳

有没有办法在PHP中执行子进程结束时获取环境状态?

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?

  • 写回答

1条回答 默认 最新

  • dongtang6775 2012-08-28 07: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.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部