Show the environment variables of a running process in Linux

Oh Dear! monitors your entire site, not just the homepage. We crawl and search for broken pages and mixed content, send alerts when your site is down and notify you on expiring SSL certificates.

Start your free 10 day trial! »

Image of Mattias Geniar

Mattias Geniar, January 11, 2017

Follow me on Twitter as @mattiasgeniar

A quick mental dump in case I forget it again next time. First, check your PID:

$ ps faux | grep 'your_process'
508      28818  0.0  0.3  44704  3584 ?        Sl   10:10   0:00  \_ /path/to/your/script.sh

Now, using that PID (in this case, 28818), check the environment variables in /proc/$PID/environ.

$ cat /proc/28818/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

Now to get that output more readable, parse the null character (\0) and replace them by new lines (\n) for readability.

$ cat /proc/28818/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

Alternatively (but equally hard to read), you can add the e modifier to ps to also show all environment variables. I personally find /prod easier to interpret, since it’s only the environment variables.

$ ps e -ww -p 28818
  PID TTY      STAT   TIME COMMAND
28818 ?        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

If you’ve got other tricks, I’d love to hear about them!

Subscribe to my newsletter

Get a weekly-ish update on Linux, open source & webdev called cron.weekly.

Will you help me share this post?

It would mean a lot to me if you could help share this post on social media. 🤗