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

Re: [Sheflug] Little script help




> 
> I think that you have to iterate through each numbered directory
> in /proc and parse /proc/<pid>/cmdline. I'm not 100% sure of its
> syntax though. At first glance, it doesn't seem to like putting
> blankspace between the command name and its commandline arguments.
> I'm sure there's probably some non-printable character in there,
> probably '\0' ;) 

That may not work either, examine the dump below:

[469%][infinitum][21434] >ps ax | grep 21434
21434 ?        SW     0:00 [httpd]
31178 pts/3    S      0:00 grep 21434
[470%][infinitum][21434] >pwd
/proc/21434
[471%][infinitum][21434] >cat cmdline
[472%][infinitum][21434] >cat status
Name:   httpd
State:  S (sleeping)
Pid:    21434
PPid:   1996
Uid:    65534   65534   65534   65534
Gid:    65534   65534   65534   65534
Groups: 65534 
VmSize:     3524 kB
VmLck:         0 kB
VmRSS:         0 kB
VmData:      560 kB
VmStk:        20 kB
VmExe:       932 kB
VmLib:      1776 kB
SigPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000001000
SigCgt: 000000000040666b
CapInh: 00000000fffffeff
CapPrm: 0000000000000000
CapEff: 0000000000000000
[473%][infinitum][21434] >


Note that cmdline is empty, for some reason. If you 'ps ax', you'll find that 
the processes in square brackets, will have an empty /proc/$$/cmdline. Why 
this is I don't know, and one I didn't know about until now. So I may dig :)

However ... in order to do a basic 'ps', a script like this does the job:

#!/bin/sh

for i in /proc/*
do
	if [ -f $i/cmdline ]
	then
		cmd=`tr '\0' ' ' < $i/cmdline`
		if [ "$cmd" = "" ]
		then
			cmd="[`grep '^Name:' $i/status | awk '{ print $2 }'`]"
		fi
		echo "`basename $i`	$cmd"
	fi
done
## here it endeth

This script could probably be speeded up a bit; it's a quick hack. But it 
gives the framework for you to build on. It may even be quicker knocking 
summat up in perl (my perl is rusty, so that would take me more than the 
couple of mins of that script).

But that should give you a starting point, Jose :)

Chris...


-- 
Chris Johnson            \  "If not for me then, do it for yourself. If not
sixie@nccnet.co.uk        \  for then do it for the world." -- Stevie Nicks
www.nccnet.co.uk/~sixie/   ~---------------------------------------+
Redclaw chat - http://redclaw.org.uk - telnet redclaw.org.uk 2000   \______


---------------------------------------------------------------------
Sheffield Linux User's Group - http://www.sheflug.co.uk
To unsubscribe from this list send mail to
- <sheflug-request [at] vuw.ac.nz> - with the word 
 "unsubscribe" in the body of the message. 

  GNU the choice of a complete generation.