My blog has moved!

You should be automatically redirected. If not, visit
http://benohead.com
and update your bookmarks.

Thursday, March 15, 2012

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 "\000" "\n"


or


tr '\0' '\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

No comments:

Post a Comment