get environment variables of running process in linux
Get the process ID using ps
command
ps -ef | grep something
For example:
root@ubuntuserver:~# ps -ef |grep xfce4-session
user01 7363 7287 0 11:17 ? 00:00:00 xfce4-session
root 57795 7644 0 11:53 pts/0 00:00:00 grep --color=auto xfce4-session
root@ubuntuserver:~#
Here the PID
is 7363
Get environment variables from file /proc/PID/environ
root@ubuntuserver:~# cat /proc/7363/environ
SHELL=/bin/bashQT_ACCESSIBILITY=1SSH_AUTH_SOCK=/tmp/ssh-JRT467Z6hAHp/agent.7287SSH_AGENT_PID=7354PWD=/home/user01LOGNAME=user01XDG_SESSION_TYPE=x11GPG_AGENT_INFO=/run/user/1002/gnupg/S.gpg-agent:0:1PULSE_SCRIPT=/etc/xrdp/pulse/default.paHOME=/home/user01LANG=en_US.UTF-8XRDP_SOCKET_PATH=/run/xrdp/sockdirXRDP_PULSE_SOURCE_SOCKET=xrdp_chansrv_audio_in_socket_10XDG_SESSION_CLASS=userUSER=user01XRDP_SESSION=1DISPLAY=:10.0XRDP_PULSE_SINK_SOCKET=xrdp_chansrv_audio_out_socket_10SHLVL=1XDG_SESSION_ID=c12XDG_RUNTIME_DIR=/run/user/1002XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktopPATH=/sbin:/bin:/usr/bin:/usr/local/bin:/snap/binDBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1002/busUID=1002_=/bin/xfce4-sessionroot@ubuntuserver:~#
/proc/[pid]/environ
This file contains the environment for the process. The entries
are separated by null bytes ('\0'), and there may be a null byte
at the end.
Print out environment variables
root@ubuntuserver:~# strings /proc/7363/environ
SHELL=/bin/bash
QT_ACCESSIBILITY=1
...
SSH_AUTH_SOCK=/tmp/ssh-JRT467Z6hAHp/agent.7287
UID=1002
root@ubuntuserver:~#
OR
root@ubuntuserver:~# cat /proc/7363/environ | tr '\000' '\n'
SHELL=/bin/bash
QT_ACCESSIBILITY=1
...
SSH_AUTH_SOCK=/tmp/ssh-JRT467Z6hAHp/agent.7287
UID=1002
root@ubuntuserver:~#
See snippet of man proc
/proc/[pid]/environ
This file contains the initial environment that was set when the currently exe‐
cuting program was started via execve(2). The entries are separated by null
bytes ('\0'), and there may be a null byte at the end. Thus, to print out the
environment of process 1, you would do:
$ cat /proc/1/environ | tr '\000' '\n'