user1337896
user1337896

Reputation: 1321

Programmatically get the environment variables of another process?

I want to get the environment variables of process A from process B? There is no relationship with A and B. Not the System enviroment variables , but the enviroment variables that have been passed to the process!

In linux, or windows

Upvotes: 3

Views: 579

Answers (2)

Jeeva
Jeeva

Reputation: 4663

The environment variables are shown in /proc/PID/environ:

$ cat /proc/19065/environ
DISPLAY=localhost:0.0SHELL=/bin/bashPWD=/home/phil...

check out this question.

Upvotes: 2

In Linux you can do this by reading (or mmaping) /proc/[pid]/environ. From proc(5):

/proc/[pid]/environ

This file contains the environment for the process. The entries are separated by null bytes ('\0'), and there may be a null byte at the end. Thus, to print out the environment of process 1, you would do:

      $ (cat /proc/1/environ; echo) | tr '\000' '\n'

Upvotes: 2

Related Questions