Waiting for processes to finish under Solaris

B

Behrang Dadsetan

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)
------------------------------------------------------------------------------
 
B

Behrang Dadsetan

Wow.. that is a pretty nice shortcut indeed.. at least for the second
step of waiting for the process to end by itself!

It is also a very underdocumented feature.. :)) signal.SIG_DFL (0) is
documented as """SIG_DFL
This is one of two standard signal handling options; it will simply
perform the default function for the signal. For example, on most
systems the default action for SIGQUIT is to dump core and exit, while
the default action for SIGCLD is to simply ignore it. """
Hopefully it will NOT SIGQUIT it...
I can only test it tomorrow but if it works it will certainly give it a
performance boost. If it does not maybe some other signal is completed
ignored...
Pity I will have to probably run the watch script as the same user than
the process. I guess I will not be able to kill it otherwise.

Could you maybe find anything where you would say, could be done a
little more pythonic? :)

Thanks.
Ben.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top