在Go中运行os/exec命令后获取环境

huangapple go评论110阅读模式
英文:

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?

huangapple
  • 本文由 发表于 2013年4月3日 11:28:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/15778363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定