How to See the Environment Variables of the Process or Application
Why do we need this?.
You are in the process of investigating an issue and seems that process is not running as expected compared with other environment or compared to previous execution.
There could be chance that your process might have started with wrong environment variables then the regular execution. To identify it you can check the process and its related environment variables when it was started
How to achieve it ?
Method 1 : Add the “e” modifier to “ps” to also show all environment variables
$ ps e -ww -p 96034
PID TTY STAT TIME COMMAND
96034 ? Sl 0:00 /path/to/your/script.sh TERM=linux PATH=/sbin:/usr/sbin:/bin:/usr/bin runlevel=3 RUNLEVEL=3 SUPERVISOR_GROUP_NAME=xxx PWD=/var/www/vhosts/worker.nucleus.be/cron-tasks LANGSH_SOURCED=1 LANG=en_US.UTF-8 previous=N
Method 2: Get the PID and access the environment file under /proc
First, check your PID:
$ ps faux | grep ‘your_process’
508 96034 0.0 0.3 44704 3584 ? Sl 10:10 0:00 _ /path/to/your/script.sh
check the environment variables in /proc/$PID/environ
$ cat /proc/96034/environ
TERM=linuxPATH=/sbin:/usr/sbin:/bin:/usr/binrunlevel=3RUNLEVEL=3SUPERVISOR_GROUP_NAME=xxxPWD=/path/to/your/homedirLANGSH_SOURCED=1LANG=en_US.UTF-8previous=NPREVLEVEL=N
to get that output more readable, you can do two things. Either parse the null character (\0) and replace them by new lines (\n) or use the strings tool that does this for you
$ cat /proc/96034/environ | tr ‘\0’ ‘\n’
TERM=linux
PATH=/sbin:/usr/sbin:/bin:/usr/bin
runlevel=3
RUNLEVEL=3
SUPERVISOR_GROUP_NAME=xxx
PWD=/path/to/your/homedir
LANGSH_SOURCED=1
LANG=en_US.UTF-8
previous=N
PREVLEVEL=N
$ strings /proc/96034/environ
TERM=linux
PATH=/sbin:/usr/sbin:/bin:/usr/bin
runlevel=3
RUNLEVEL=3
SUPERVISOR_GROUP_NAME=xxx
PWD=/path/to/your/homedir
LANGSH_SOURCED=1
LANG=en_US.UTF-8
previous=N
PREVLEVEL=N