Sometimes it may be useful to see what environment variables were passed to a process. e.g. you may want to see LC_ALL value for a long running or command or environment variable passed to a cron process. Here are the steps to find env variables passed to a process given its pid on Ubuntu Linux. This approach should work on other Linux systems also.
Each process keeps the list of environment variables in file /proc/[pid]/environ. Here are quick steps to find environment variables for running apache2 daemon.
-
First find the pid of running apache2 process.
$ ps aux | grep apache2 | grep root root 27220 0.0 3.1 432004 31892 ? Ss Oct04 0:39 /usr/sbin/apache2 -k start
-
Now run the following to display environment variable for above process id:
$ sudo cat /proc/11824/environ | tr '\0' '\n' APACHE_RUN_DIR=/var/run/apache2 APACHE_PID_FILE=/var/run/apache2/apache2.pid PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin APACHE_LOCK_DIR=/var/lock/apache2 LANG=C APACHE_RUN_USER=www-data APACHE_RUN_GROUP=www-data APACHE_LOG_DIR=/var/log/apache2 PWD=/
You can also use Linux cli utility strings to print content of /proc/[pid]/environ in human readable format as shown below:
$ sudo strings /proc/11824/environ