I have a very short process for which i want to check the environmental variables for debugging purposes. The process is instant from a human perspective. The example illustrates it better.


user:~$ ps aux | grep grep
user   2349  0.0  0.0   4384   840 pts/1    S+   14:47   0:00 grep --color=auto grep

The pid of the process was 2349. If I check the environmental variables (or other info):

#Pretty print of environmental variables#
user:~$ xargs --null --max-args=1 echo < /proc/2349/environ
bash: /proc/2349/environ: No such file or directory

The info has vanished with the process.

What to do before running the process to be able to list the variables once the precess is finished?


if you have strace

strace -v -e execve cmd

If the values of the variables are too long you can increase the string size with -s

strace -v -s 10000 -e execve cmd

I would move the binary to one side and create a wrapper that dumps the environment to a file and then calls the original.


mv ${cmd} ${cmd}.moved

Then create a new file ${cmd}:

env > /tmp/cmd-env.log
exec ${cmd}.moved "$@"

And, don't forget the permissions:

chmod a+rx ${cmd}

Then, wait for it to run again, and read the environment from /tmp/cmd-env.log.

Finally, put the original program back where it came from:

mv ${cmd}.moved ${cmd}

Of course, it may be possible to avoid moving the binary by putting the wrapper in another bin directory earlier in $PATH.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.