英文:
Get environment after running an os/exec command in Go
问题
我正在通过os.exec包运行外部命令。我可以使用Cmd.Env成员为外部命令指定环境。然而,该命令可能会修改该环境,我希望能够捕获这些修改以供后续调用该命令时使用。
我如何在命令终止后立即找出环境的状态?
英文:
I am running an external command via os.exec package. I can specify an environment for the external command using the Cmd.Env member. However, the command may modify that environment and I want to capture those modifications for subsequent invocations of the command.
How do I find out the state of the environment immediately after the command terminates?
答案1
得分: 4
我不相信有一种标准接口可以以平台无关的方式实现你想要的功能。
例如,在像Linux这样的系统上,进程的环境是通过进程地址空间中的environ
全局变量来管理的。只有当这个环境传递给execve
系统调用时,本地环境的更改才会传播到新的进程。
所以要实现你想要的功能,你真的需要执行的进程的合作。也许你可以让它在收到信号时将其环境写出来?
英文:
I don't believe there is a standard interface to do what you're after in a platform independent way.
On systems like Linux for instance, the process's environment is managed through the environ
global variable in the process's address space. Changes to the local environment only propagate to new processes because this environment is passed to the execve
system call.
So to do what you want, you really need the cooperation of the process you are executing. Perhaps you could have it write out its environment in response to a signal?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论