Friday, January 2, 2009

Environment variables a process runs with

Recently I had a problem with a certain process that could be launched it two different ways: from command line and from a graphical client application. When launched from the command line it didn't quite work, but it did when started via the graphical client application. I knew that it was an environment variable issue, but I didn't know how I could find out with which environment variables a certain process was running.
I guessed that I could find something it the /proc directory, so I got into and found that for each running process there's a file called 'environ', which can be found in /proc/PID/environ. Ok, so that's fine and it would be a great solution, provided my problem was in a linux box, but it was Solaris 5.9 machine, and in solaris the /proc file, though it exists, it's not so full of information as in linux. And there was no environ file.
Fortunately I found on the web (I don't have the url anymore, sorry) that you can do something like:

ps wwe
to get the what I was looking for, and this works on linux and solaris. For example, to get the environment of my running bash session:

testing@fixdebian:~$ ps wwe $$

PID TTY STAT TIME COMMAND
27857 pts/6 S 0:00 -su TERM=xterm PATH=/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games LANG=es_ES@euro MAIL=/var/mail/testing HOME=/home/testing SHELL=/bin/bash USER=testing LOGNAME=testing


which is far more elegant. And this solved my problem in a more general way.

No comments: