Operating System - HP-UX
1822734 Members
3913 Online
109644 Solutions
New Discussion юеВ

obtaining environment variables for any process

 
David Calkins
New Member

obtaining environment variables for any process

I would like to obtain the environment variables for another process. Ideally this would be done by providing the PID of any running process, and getting the environment variables back.

It is easy to get the environment variables for the current process, but I have been unable to figure out a way to get this information for another process.

Any help would be greatly appreciated :-)

Thanks :-)
10 REPLIES 10
Anthony deRito
Respected Contributor

Re: obtaining environment variables for any process

Take a look at printenv. Use it when a process executes and write contents to a file. The env comamnd will allow you to do the same and also edit the environment on-the-fly.

Tony
David Calkins
New Member

Re: obtaining environment variables for any process

The commands you mentioned work on the current process :-( Not another process. I need to be able to get the environment variables for another process.
Anthony deRito
Respected Contributor

Re: obtaining environment variables for any process

Hello, David.

Sorry I cannot help. However, you may get more hits from this forum if you state what you are trying to accomplish.

Tony
David Calkins
New Member

Re: obtaining environment variables for any process

My goal is as follows: I have a whole set of processes. I can easily obtain the PIDs for them. What I would like is to know the values of certain environment variables as seen by these individual processes. If I know this, I can then pick out all the processes that have a certain value for a given variable.

For example, the DISPLAY variable. Suppose I wanted to pull out all processes with a certain value for the DISPLAY variable.

So, this is why I would like to access the environment variables for another process.

Thanks for your replies :-)

Re: obtaining environment variables for any process

Hi David,

I don't know a way to get environment variables of processes, but if you do a
who -R
you get the IP-adresses of the terminals where users login from
If you match the tty and pts names of the users with the tty and pts names of the ps -ef output, you may get what you want.

Regard, Andre
RikTytgat
Honored Contributor

Re: obtaining environment variables for any process

Hi,

You might try the pstat_getproc(2) call. It retrieves information about one or more processes from the kernel structures.

I don't have a HP-UX server at hand right now, so I can't exactly tell what kind of information it retrieves. You should have a look at the pst_status struct in /usr/include/sys/pstat.h

The manpage for pstat contains examples on how to use the series of system calls.

Hope this helps,
Rik.
Manju Kampli
Trusted Contributor

Re: obtaining environment variables for any process

since when ever a process is started by a user, the process gets the default environment variables from the user home directory file .kshrc for ksh and .cshrc for csh. May be you can get the process owner and look for these files for the process environment. I know this will not apply for the cases where the environment for a process is set after it has read from .kshrc/.cshrc file
Never stop "LEARNING"
Shannon Petry
Honored Contributor

Re: obtaining environment variables for any process

Hi David,

Can you give a better example of what you need to do?
I dont think that you can do what you need to, unless you are running something like screens, which allows connects and re-connects to the same tty, but can not give any work arounds without more information.

I.E.
I have used my own scripts for tracking things. I.E. A user running certain programs. I re-name the program, and create a script which launches the program, but only after logging the username, date, time, environment, etc....The log is mailed to me, or dumped to the system, and the user never knows the better :)
Best regards
Shannon
Microsoft. When do you want a virus today?
Douglas Nakamoto
Occasional Advisor

Re: obtaining environment variables for any process

I can't think of a way to give you the 'ideal' situation you mention, but if you have the authority to cause changes to be made to all .profile files -- I can recommend a 'jury-rig' method.

Basically, if you were to add a line to each .profile to echo the current environment (env, set, etc.) to a file, this file could be examined for the environment.

Also, if you have even more control over your machine & users -- you could even set up a script to be used instead of the "export" command. This script would echo all new exported environment variables to the user's 'environment' file (created per the previous paragraph) before it actually exported the environment variable.

I don't know if this would be worth your while or give you what you need -- but, it is an option.
Devbinder Singh Marway
Valued Contributor

Re: obtaining environment variables for any process

hi,

when a user logs is depends on what shell they are using for example ksh , if there is an /etc/profile that runs first and the if there is a local .profile that runs. Environment variables maybe set within these profiles , for specific users or a global entry depends which profile you are running.

One way you can check the environment variables is in the users profile before the application is started put in a sh ( indicates go to shell) and then typer in env
this will list environment variables set for that particular user ..

hope this helps

Dev
Seek and you shall find