[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[seul-edu] [Fwd: [Mlug] Process Environment in Linux /proc]



Mike et el,

I don't understand much of this, they're on the same topic so I'm
sending this along.

all the best,

Gordon

On Tue, Feb 26, 2002 at 10:21:06PM -0500, Ralph M. Deal wrote:
> > # du / | sort -rn | less
> I get 
> du: `/;proc/26785/fd/4': No such file or directory
> Frustrating.
Just "spurious".  Don't try to shrink footprint by
rm -f /proc/kcore like I tried to several years ago :-)

/proc is a land of its own rules.

-- 
 ---- WBR, Michael Shigorin <mike@altlinux.ru>

-------- Original Message --------
Subject: [Mlug] Process Environment in Linux /proc
Date: Wed, 27 Feb 2002 12:00:52 +0000
From: Nick Sklav <sklav@mlug.ca>
Reply-To: mlug@mlug.ca
Organization: Montreal Linux User Group
To: mlug@mlug.ca


Recovering the Process Environment in Linux
By Sandra Henry-Stocker

Ever wish that you had more information about a running process than
what you see when you issue the "ps" command? On a Linux system, a
considerable amount of information about the processing environment
that was in effect when a process was started is available for perusing
through the /proc file system. Like Solaris, Linux provides this pseudo
filesystem so that information about running processes is more
accessible to users.

If you cd over to the /proc directory, you'll notice that there are
many directories with numeric names. You'll probably also notice that
these directories are not like other Unix directories -- they all have
a size of 0 bytes. This is because they are not "real" directories that
take up space on your hard drives, but pseudo directories (i.e.,
operating system constructs that provide a familiar interface to
information that might otherwise be difficult to access.

Each of these numerically named directories corresponds to the process
Ids of a particular process running on your system. A "ps -ef |
awk '{print $2}'" command will list the same information. Where the
output from the ps command might look like this:

[shs@dragonfly proc]$ ps -ef | awk '{print $2}'
PID
1
2
3
4
5
6
7
8
16
91
185
186
187
188
.. and so on

the corresponding directories will look like this:

dr-xr-xr-x    3 root     root            0 Feb 17 17:26 1
dr-xr-xr-x    3 root     root            0 Feb 17 17:26 16
dr-xr-xr-x    3 root     root            0 Feb 17 17:26 2
dr-xr-xr-x    3 root     root            0 Feb 17 17:26 3
dr-xr-xr-x    3 root     root            0 Feb 17 17:26 4
dr-xr-xr-x    3 root     root            0 Feb 17 17:26 5
dr-xr-xr-x    3 root     root            0 Feb 17 17:26 6
dr-xr-xr-x    3 root     root            0 Feb 17 17:26 7
dr-xr-xr-x    3 root     root            0 Feb 17 17:26 8
dr-xr-xr-x    3 root     root            0 Feb 17 17:26 91
dr-xr-xr-x    3 root     root            0 Feb 17 17:29 185
dr-xr-xr-x    3 root     root            0 Feb 17 17:29 186
dr-xr-xr-x    3 root     root            0 Feb 17 17:29 187
dr-xr-xr-x    3 root     root            0 Feb 17 17:29 188

Zero length or not, these pseudo directories contain interesting files,
also with lengths of zero. If we were to cd into /proc and list the
contents of one, we would see something like this:

[shs@dragonfly proc]$ cd 9166
[shs@dragonfly 9166]$ ls -l
total 0
-r--r--r--    1 shs      shs             0 Feb 17 17:31 cmdline
lrwxrwxrwx    1 shs      shs             0 Feb 17 17:31 cwd -> /home/shs
-r--------    1 shs      shs             0 Feb 17 17:31 environ
lrwxrwxrwx    1 shs      shs             0 Feb 17 17:31 exe -
 > /usr/bin/gnome-terminal
dr-x------    2 shs      shs             0 Feb 17 17:31 fd
-r--r--r--    1 shs      shs             0 Feb 17 17:31 maps
-rw-------    1 shs      shs             0 Feb 17 17:31 mem
lrwxrwxrwx    1 shs      shs             0 Feb 17 17:31 root -> /
-r--r--r--    1 shs      shs             0 Feb 17 17:31 stat
-r--r--r--    1 shs      shs             0 Feb 17 17:31 statm
-r--r--r--    1 shs      shs             0 Feb 17 17:31 status

Each of these files provides access to different information about the
particular process.

cmdline  cwd  environ  exe  fd  maps  mem  root  stat  statm  status

The usefulness of this information might not be readily apparent, but
if you find value in the output of the ps command, you're likely to
find value in the /proc files.

You might, for example, see a vi process like that shown below in your
ps output and wonder about the file system location of the file being
edited.

shs      13674 13457  0 22:37 pts/1    00:00:00 vi 02-18-env

You could search for a file with the name shown, but where do you start
on a large system? How long will it take?  The first line in the
command below shows you the directory that was the current working
directory when the process was initiated.

[root@dragonfly 13674]# cat environ | tr "\000" "\n" | more
PWD=/home/shs/sysadmin
XAUTHORITY=/home/shs/.Xauthority
WINDOWID=29360263
HOSTNAME=dragonfly
.. and so on ...

The environ file in each /proc subdirectory contains information about
the environment in which the process was started. We use the tr
(translate) command to change the null characters that separate each
field into linefeeds to make our display more readable.

The cmdline files shows the complete command as it was typed (unless
the process has been swapped out of memory).

The cwd file is a symlink to the working directory when the process was
started.

The exe file points to the binary process being run.  For example:

lrwxrwxrwx    1 root     root     0 Feb 17 17:41 exe -> /usr/bin/gdm

The fd file, as you'd expect, shows the file descriptors being used --
with a symbolic link pointing to each of the files (this by itself is
very interesting).

The maps file contains descriptive information on the currently mapped
memory regions.

The root file points to / or to a chroot'ed file system location (if
one was in effect when the command was invoked), using another symbolic
link.

lrwxrwxrwx    1 root     root            0 Feb 17 17:41 root -> /

The status file contains information such as this:

[shs@dragonfly 1115]$ cat status
Name: 
	 gdm
State: 
	 S (sleeping)
Pid: 
	 1115
PPid: 
	 1

Whether or not you're going to log on to your Linux system right now to
see what new information you can glean about running processes, you
will probably find some interesting uses for the files in /proc.

The command in this column were run on a RedHat 7.2 system.

About the author(s)
-------------------
Sandra Henry-Stocker (a.k.a S. Lee Henry) has administered Unix systems
for over 17 years. In fact, she describes herself as "USL" (Unix as a
second language) and barely remembers enough English to write books and
buy groceries. She currently works for TeleCommunications Systems, a
wireless technology company in Annapolis, MD.
_______________________________________________
Mlug mailing list
Mlug@mlug.ca
http://lists.mlug.ca/mailman/listinfo/mlug