27th January 2019
Finding the Environment Variables of another process
With a Linux system, you can get a lot of information about a process from the /proc
pseudo filesystem. Each process has an integer number to represent it; this is known as its Process ID, or PID. You see this in the output from the ps
command and in many other places. A shell will report its own PID in the $$ variable, though you can also query your own /proc
entry via /proc/self
- the kernel will treat /proc/self
as the calling program's PID.
Be careful in testing this, though: If you try "ls -l /proc/self/exe
", you will call ls
, and the ls
program will call /proc/self/exe
, which gives you the name of the ls
program itself, and not your shell's executable!
steve@pantera:~$ ls -l /proc/self/exe lrwxrwxrwx 1 steve steve 0 Jan 27 01:12 /proc/self/exe -> /bin/ls steve@pantera:~$
Inspecting another process
That detail aside, you can check the status of another process entirely, if either (a) you are the root
user, or (b) it is a process owned by you. (You can't spy on other users' processes)
If you want to know about a process with a PID of 31824, you can check it by viewing its /proc/PID/environ
file:
steve@pantera:~$ cat /proc/31824/environ LANG=en_GB.UTF‑8DISPLAY=:0XDG_VTNR=2LOGNAME=stevePWD=/home/steveXAUTHORITY=/run/user/1000/gdm/XauthorityQT_LINUX_ACCESSIBILITY_ALWAYS_ON=1QT_QPA_PLATFORMTHEME=qgnomeplatformJOURNAL_STREAM=8:23436COLORTERM=truecolorXDG_SESSION_ID=2DESKTOP_SESSION=defaultXDG_SESSION_DESKTOP=defaultGDMSESSION=defaultGNOME_DESKTOP_SESSION_ID=this‑is‑deprecatedUSERNAME=steveWINDOWPATH=2DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/busVTE_VERSION=4601XDG_DATA_DIRS=/usr/share/gnome:/usr/local/share/:/usr/share/GJS_DEBUG_TOPICS=JS ERROR;JS LOGXDG_MENU_PREFIX=gnome‑QT_ACCESSIBILITY=1GDM_LANG=en_GB.UTF‑8GJS_DEBUG_OUTPUT=stderrXDG_SESSION_TYPE=x11SHELL=/bin/bashWINDOWID=18874374TERM=xterm‑256colorGTK_MODULES=gail:atk‑bridgeSSH_AUTH_SOCK=/run/user/1000/keyring/sshXDG_CURRENT_DESKTOP=GNOMEPATH=/home/steve/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/gamesSSH_AGENT_PID=1776HOME=/home/steveXDG_SEAT=seat0XDG_RUNTIME_DIR=/run/user/1000SESSION_MANAGER=local/pantera:@/tmp/.ICE‑unix/1711,unix/pantera:/tmp/.ICE‑unix/1711GPG_AGENT_INFO=/run/user/1000/gnupg/S.gpg‑agent:0:1USER=steve steve@pantera:~$
Unfortunately, that is not very easy to read. The values are split by an ASCII "null" character (which is not a printable character), so all the variable names and their values get squashed in to each other.
There are a few ways around this, but one of the neatest is to use the xargs
command. This command has the convenient abiliy to be told which ASCII character to use as a seperator. So if you throw the content towards "xargs -0
", then xargs
will helpfully split the words at the otherwise unprintable "null" boundary.
That will pad the items with spaces. However, you can make this easier to read by telling xargs
to process each item a line at a time, with the "xargs -0 -n1
" option:
steve@pantera:~$ xargs -n1 -0 < /proc/31824/environ LANG=en_GB.UTF-8 DISPLAY=:0 XDG_VTNR=2 LOGNAME=steve PWD=/home/steve XAUTHORITY=/run/user/1000/gdm/Xauthority QT_LINUX_ACCESSIBILITY_ALWAYS_ON=1 QT_QPA_PLATFORMTHEME=qgnomeplatform JOURNAL_STREAM=8:23436 COLORTERM=truecolor XDG_SESSION_ID=2 DESKTOP_SESSION=default XDG_SESSION_DESKTOP=default GDMSESSION=default GNOME_DESKTOP_SESSION_ID=this-is-deprecated USERNAME=steve WINDOWPATH=2 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus VTE_VERSION=4601 XDG_DATA_DIRS=/usr/share/gnome:/usr/local/share/:/usr/share/ GJS_DEBUG_TOPICS=JS ERROR;JS LOG XDG_MENU_PREFIX=gnome- QT_ACCESSIBILITY=1 GDM_LANG=en_GB.UTF-8 GJS_DEBUG_OUTPUT=stderr XDG_SESSION_TYPE=x11 SHELL=/bin/bash WINDOWID=18874374 TERM=xterm-256color GTK_MODULES=gail:atk-bridge SSH_AUTH_SOCK=/run/user/1000/keyring/ssh XDG_CURRENT_DESKTOP=GNOME PATH=/home/steve/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games SSH_AGENT_PID=1776 HOME=/home/steve XDG_SEAT=seat0 XDG_RUNTIME_DIR=/run/user/1000 SESSION_MANAGER=local/pantera:@/tmp/.ICE-unix/1711,unix/pantera:/tmp/.ICE-unix/1711 GPG_AGENT_INFO=/run/user/1000/gnupg/S.gpg-agent:0:1 USER=steve steve@pantera:~$
Now you get a nice, easy to read view of the environment of another process on the system.
Summary
The /proc/PID/environ
pseudo-file is created for you by the Kernel, but is not too easy to read, in the way it is presented.
By using xargs -0 -n1
, you can easily format it in a more easily readable format, and indeed this can be easily passed to another process.