Waiting for processes to finish under Solaris

Behrang Dadsetan ben at dadsetan.com
Tue Jul 15 14:40:50 EDT 2003


Hi all,

please note that once I finished writing the script someone explained me 
that there is a command pwait under Solaris... anyway it was fun to 
write and it did not take long (while I am not a python guru..). And my 
version is MUUUCH better :)


Is there no standard interfaces to the process table? I only found 
examples of how to do it on the win32 platform. So I parse with re the 
output of /usr/ucb/ps...

Because I am still at the begining of learning python, I wanted to have 
some advice here how I could have done the following code better...

Thanks in advance, Ben.
------waitPID.py---------------------------------------------
#!/usr/local/bin/python

import os
import time
import re
import getopt
import sys

def usage():
     print >>sys.stderr, sys.argv[0] + " Usage"
     print >>sys.stderr, """waits until the specified processes have 
finished
-r <regexp> | --regexp=<regexp>     will watch the processes matching with
                                     <regexp> at their beginning
-p <pid>    | --pid=<pid>           will watch the processes with the pid
<pid>
-h          | --help                will display this screen
"""

try:
     opts, args = getopt.getopt(sys.argv[1:], "hr:p:", ["help", "regexp=",
"pid="])
except getopt.GetoptError:
     usage()
     sys.exit(2)
regexps = []
pids = []
for o, a in opts:
     if o in ("-r", "--regexp"):
	regexps.append(re.compile('^' + a))
     if o in ("-p", "--pid"):
	pids.append(a)
     if o in ("-h", "--help"):
	usage()
	sys.exit()

def ps():
     stdout = os.popen("/usr/ucb/ps -auxwwww")
     allprocesses = stdout.readlines()
     stdout.close()
     return allprocesses


_psline =
re.compile(r"^(\S{1,10})\s*(\d+)\s+(\d+\.\d+)\s+(\d+\.\d{1,2})\s*(\d{1,6})\s
*(\d{1,8})\s+(\?|\S+)\s+([A-Z])\s+(\d+:\d+:\d+|\S\S\S
\d+)\s+(\d+:\d+)\s+(.*)$")
def splitpsline(line):
     match = _psline.search(line)
     if match:
	owner, pid, cpu, mem, sz, rss, tt, s, start, time, command =
match.groups()
	return owner, pid, cpu, mem, sz, rss, tt, s, start, time, command

watchedforpids = {}

processesmatrix = [ p for p in map(splitpsline, ps()) if p ]

for owner, pid, cpu, mem, sz, rss, tt, s, start, cputime, command in
processesmatrix:
     basenamecmd = os.path.split(command)[1]
     for watchedpid in pids:
	if watchedpid == pid:
	    watchedforpids[pid] = command

     for watchedforcmd in regexps:
	if watchedforcmd.search(command) or
watchedforcmd.search(basenamecmd):
	    watchedforpids[pid] = command


while 1:
     time.sleep(2)
     foundpids = {}
     processesmatrix = [ p for p in map(splitpsline, ps()) if p ]
     for owner, pid, cpu, mem, sz, rss, tt, s, start, cputime, command in
processesmatrix:
	for watchedpid in watchedforpids.keys():
	    if watchedpid == pid:
		foundpids[pid] = command
     if not foundpids:
	break
     print
     for pid, command in foundpids.items():
	print "PID[%s]:COMMAND[%s] still alive" % (pid, command)
     time.sleep(58)
------------------------------------------------------------------------------





More information about the Python-list mailing list