How to get process info from python
gardner at networknow.org
Wed Jun 9 02:03:16 CEST 2004
AIX does not seem to support the -o format syntax. What I am using is
"ps -lf" which seems to give me as much information as possible. I also,
optionally, add "-u user" if I need to get all the information for a user,
instead of just for the current tty process tree.
Currently, all I am using is the cmd line, but since there doesn't seem to
be any other way to get the memory information for the process from python,
I will need to start using the SZ column. Here is what I have so far
(hopefully 129 lines of code is not too much to post in the group. If it is,
from optparse import OptionParser
class ProcessStatus :
self.uid = ''
self.pid = ''
self.ppid = ''
self.priority = ''
self.nice = ''
self.size = ''
self.start = ''
self.time = ''
self.cmd = ''
headings = header.split()
# ----- find the char position of the WCHAN, STIME
# ----- TTY and CMD headings
wchan_pos = string.index(header,'WCHAN')
stime_pos = string.index(header,'STIME')
tty_pos = string.index(header,'TTY')
cmd_pos = string.index(header,'CMD',stime_pos)
# ----- cut the status line before WCHAN because
# ----- that is often blank
n = string.rindex(status,' ',0,wchan_pos)
status1 = status[:n-1]
fields = status1.split()
if string.find(status,'<defunct>') < 0 :
# ----- the stime is all the chars between
# ----- STIME and TTY
x = string.rindex(status,' ',0,stime_pos)
y = string.rindex(status,' ',stime_pos,tty_pos)
sTime = string.strip(status[x:y])
# ----- the TTY and TIME fields are all the chars
# ----- between TTY and CMD
max_pos = cmd_pos + 5
if max_pos > len(status)-1 :
max_pos = len(status) - 1
m = string.rindex(status,':',tty_pos,max_pos)
n = string.index(status,' ',m)
status2 = status[y+1:n].strip()
# ----- CMD is the rest of the line
status3 = status[n:].strip()
for h,y in map(None,headings,fields) :
if h == 'UID' :
self.uid = y
elif h == 'PID' :
self.pid = y
elif h == 'PPID' :
self.ppid = y
elif h == 'PRI' :
self.priority = y
elif h == 'NI':
self.nice = y
elif h == 'SZ' :
self.size = y
elif h == 'STIME' :
self.start = y
elif h == 'TIME' :
self.time = y
elif h == 'CMD' :
if string.find(status,'<defunct>') >= 0 :
self.cmd = '<defunct>'
self.cmd = y
except Exception, error :
logger = emea.get_logger()
def __str__(self) :
return "UID %s PID %s PPID %s PRI %s NI %s SZ %s STIME %s TIME %s
CMD %s" \
def ps(user=None) :
options = ''
if user == 'all' :
options += ' -e'
elif user != None :
options += ' -u ' + user
cmd = 'ps -lf ' + options
ps_result = commands.getoutput(cmd)
lines = ps_result.splitlines()
psList = 
for l in lines[1:] :
if __name__ == "__main__" :
parser = OptionParser()
help="user to do ps for")
default="emea_auto.log",help="name of log file")
help="verbose mode (for debugging)")
(options,args) = parser.parse_args()
# ----- define logging
psList = ps(options.user)
for p in psList:
> -----Original Message-----
> From: python-list-bounces+gardner=networknow.org at python.org
> [mailto:python-list-bounces+gardner=networknow.org at python.org]On Behalf
> Of Nick Vargish
> Sent: Tuesday, June 08, 2004 10:52 AM
> To: python-list at python.org
> Subject: Re: How to get process info from python
> "Gardner Pomper" <gardner at networknow.org> writes:
> > On a related issue, I was very surprise to see that
> os.loadavg() had been
> > added in version 2.3. This seems just as OS specific as the ps
> > I want, so that raised my hopes.
> I've had to work with process data in the past, and you're right, a
> module to handle accessing ps data would be quite useful. Just for
> fun, I've started writing something to help. All the systems that I
> use "ps" on support a "-o format" for extracting specific information,
> is this option supported on AIX?[*]
> What sort of information do you need from ps? I'll try to work it into
> the module's functionality.
> I can't promise when it will be ready, but you seem to be rolling your
> own solution anyway.
> [*] Linux, OS X, and Tru64 (nee DEC Unix nee OSF/1). I don't have
> access to an AIX system right now.
> # sigmask || 0.2 || 20030107 || public domain || feed
> this to a python
> print reduce(lambda x,y:x+chr(ord(y)-1),'
More information about the Python-list