1

How can I get the following information for a running Linux process:

  • shell environment variables
  • working directory
  • command line options

2 Answers 2

4

You can get this information from /proc filesystem, it stores information about running processes.

  • cat /proc/<pid>/environ
  • cd /proc/<pid>/cwd; pwd -P
  • cat /proc/<pid>/cmdline
2
  • cd /proc/<pid>/cwd; pwd -P, or shorter readlink -e /proc/<pid>/cwd
    – xenoid
    Aug 29, 2017 at 11:50
  • or even shorter, pwdx <pid>
    – atype
    Aug 29, 2017 at 11:59
2

If you output the environment as suggested by @atype, you get all environment variables and their values concatenated without separator.

For better output, use

$ xargs -0 -L 1 echo < /proc/21645/environ 
LANG=en_US.UTF-8
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
SHELL=/bin/bash
TERM=xterm
LC_MESSAGES=en_US.UTF-8
LANGUAGE=en_US.UTF-8

(output shortened for privacy reasons)

Explanation

/proc/$pid/environ contains the environment variables as a null-separated list.

xargs is a tool to read a arbitrarily long list from *STDIN and feeds its elements to a tool (echo in this case) ensuring to not exceed the maximum command line length. The parameter "-0" switches xargs to use null as item separator (instead of the default blank or newline), The parameter "-L 1" limits the number of items to 1. Thereby each environment variable is output on a new line.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .