Comparing process environments

Using the entry in the proc-Filesystem of a running process, it's easy to get a sorted list of environment variables for a certain process ID (PID):

tr '\0' '\n' < /proc/<PID>/environ | sort

Since the lines in environ are null-terminated, putting proper newlines in the output with tr is necessary before sorting. (sort can only output lines terminated with \0 using the option -z, but not read them, besides: even -z doesn't quite work as expected in my case)

Using the output of above command line comparing two running processes is easy, using diff or vimdiff (even remotely, see my last blog post)

Comments

Wolfgang Karall-Ahlborn's picture

Thanks to some feedback (thanks, Roland!), I came to realize that sort does actually work just fine in respect to its -z option:

sort -z < /proc/<PID>/environ | tr '\0' '\n'

does produce sorted output and as additional bonus now also works for variables with multi-line content. Silly me forgot to transliterate the null-chars to newlines after the sorting. Smile