How do you find the environment for a running process?

Linux


cat /proc/<pid>/environ

Even though /proc/<pid>/environ has a size of 0, running a cat will still return the environment for this process. This file is just a kind of link to the actual location where the enviroment for this process is stored.

The ouput of the command above is not very readable since all strings are separated by NUL and not newlines. To convert them to newlines, you can use:

strings -f /proc/<pid>/environ


or


(cat /proc/<PID>/environ; echo) | tr "00" "n"


or


tr '' 'n' < /proc/<pid>/environ


or


xargs --null --max-args=1 echo < /proc/<pid>/environ


or


cat /proc/<pid>/environ | perl -pne '$a=chr(0);s/$a/n/g'


Note: you can get the pids of a process by using 
$ pidof httpd


This return a space separated list of the PIDs of all httpd processes.


Mac OS X


ps -p <pid> -wwwE


AIX


ps eww <pid>


Solaris


pargs -e <pid>


HP-UX


The only way is to attach gdb to the process and examine _environ.


Windows

On Windows, you can do this by using a tool such as ProcessExplorer to select a particular process and view the values of the environment variables.

You could also write a small .NET program using:
System.Diagnostics.Process.GetProcessById(<pid>).StartInfo.EnvironmentVariables

One thought on “How do you find the environment for a running process?

Leave a Reply

Your email address will not be published. Required fields are marked *