Hypercipient

Determining Environment Variables of a Process

Here is the situation: You are troubleshooting an issue with a Linux process and need to determine the environment variables with which it was launched. The process does not have an interface (CLI or otherwise) that can be used to display its environment.

Solution

Inspect the details Linux stores in /proc/<pid>/environ.

Procedure

This section enumerates the steps to view the environment variables of a process.

Step 1. Determine the ID of the process

Use ps to determine the process id (pid). In this example, cron will be used.

CRON_PID=$(ps ax | grep cron | grep -v grep | awk '{ print $1 }')

The pid is now set in the environment variable CRON_PID

Step 2. Output the environment variable

Linux stores the environment variables of the process in /proc/<pid>/environ. The following command will show the values in a user-friendly way.

sudo cat /proc/${CRON_PID}/environ | tr '\0' '\n'
LANG=en_US.UTF-8
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/snap/bin
USER=root
INVOCATION_ID=...
JOURNAL_STREAM=...
SYSTEMD_EXEC_PID=1856
MEMORY_PRESSURE_WATCH=/sys/fs/cgroup/system.slice/cron.service/memory.pressure
MEMORY_PRESSURE_WRITE=...

The location of the proc pseudo-filesystem is typically mounted to /proc. Its location can be determine using the mount command if it happens to be at a different location.

mount | grep "type proc"
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)

The proc filesystem type provides an interface to the kernel data structures.

Reference

proc man page

Tags: