How to search for the sessions that have a certain environment variable set?

Hi all,

In Solaris 10, is there a way to search for the sessions that have a certain environment variable set?

Does Solaris 10 have the /proc file system?
This works on linux:

sudo grep "TERM=linux" /proc/*/environ
Binary file /proc/1258/environ matches
Binary file /proc/1378/environ matches
Binary file /proc/1380/environ matches
Binary file /proc/1396/environ matches
Binary file /proc/1397/environ matches
Binary file /proc/1420/environ matches
Binary file /proc/1/environ matches
Binary file /proc/966/environ matches
Binary file /proc/self/environ matches
Binary file /proc/thread-self/environ matches

Try

env

or

 cat ~.profile

or

/etc/default

If by sessions you mean processes, this should be what you look for (replace VARIABLE by the name of the variable you are interested in):

for i in /proc/*; do
    pargs -e $i | grep VARIABLE= && ps -f -p $(basename $i)
done 2>/dev/null

Do it as root if you need to search processes you do not own.

All,

Thanks for your suggestions. I did a test with the DISPLAY variable, I have set it in my session and I get to see it when i use env, however when I look for it with pargs -e it's not there.

I'm looking to find the PID of the session that uses a certain value for the DISPLAY variable, so I need to search after the value of the variable also.

Have you set it in a shell? Then you need to export it.

bash$ export FOO=bar
bash$ pargs -e $$ | grep FOO=
envp[7]: FOO=bar

and is also found with jlliagre's method.

I have exported the DISPLAY variable but for some reason it doesn't show up.

user@machine:/home/user>env
_=/usr/bin/env
SSH_TTY=/dev/pts/1
PATH=/usr/local/bin:/usr/bin:/bin
EDITOR=vi
LOGNAME=user
MAIL=/usr/mail/user
HOSTNAME=machine
USER=user
DISPLAY=10.5.205.232:236
SHELL=/usr/bin/ksh
HOME=/home/user
SSH_CONNECTION=129.121.221.47 53939 10.13.22.201 22
SSH_CLIENT=129.121.221.47 53939 22
TERM=xterm
PWD=/home/user
TZ=US/Central
ENV=/home/user/.kshrc
user@machine:/home/user>pargs -e $$
2925:   -ksh
envp[0]: USER=user
envp[1]: LOGNAME=user
envp[2]: HOME=/home/user
envp[3]: PATH=/usr/bin:/bin
envp[4]: MAIL=/var/mail//user
envp[5]: SHELL=/usr/bin/ksh
envp[6]: TZ=US/Central
envp[7]: SSH_CLIENT=129.121.221.47 53939 22
envp[8]: SSH_CONNECTION=129.121.221.47 53939 10.13.22.201 22
envp[9]: SSH_TTY=/dev/pts/1
envp[10]: TERM=xterm

jlliagre's method shows processes with the variable already present in the environment of a process when it was started. Just setting and exporting the variable in a shell is not enough, because the variable was not there when the shell was started. But it will show processes started by the shell.

sol10:/root # for i in /proc/*; do
    pargs -e $i | grep SOMETHING= && ps -f -p $(basename $i)
done 2>/dev/null

sol10:/root # export SOMETHING=foo

sol10:/root # for i in /proc/*; do
    pargs -e $i | grep SOMETHING= && ps -f -p $(basename $i)
done 2>/dev/null

sol10:/root # sleep 1000 &
[1]     10437

sol10:/root # for i in /proc/*; do
    pargs -e $i | grep SOMETHING= && ps -f -p $(basename $i)
done 2>/dev/null

envp[17]: SOMETHING=foo
     UID   PID  PPID   C    STIME TTY         TIME CMD
    root 10437 10087   0 11:43:41 pts/2       0:00 sleep 1000

Yes, that's exactly the issue here. Exporting a variable does really export it, it just mark it as exported. That means the variable will will be in the environment of commands that will be launched in the future but doesn't affect the shell current environment which is fixed at startup time (envp), just like its arguments (argv).