Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Waiting for processes to finish under Solaris

Reply
Thread Tools

Waiting for processes to finish under Solaris

 
 
Behrang Dadsetan
Guest
Posts: n/a
 
      07-15-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:", ["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(5
------------------------------------------------------------------------------

 
Reply With Quote
 
 
 
 
Behrang Dadsetan
Guest
Posts: n/a
 
      07-15-2003
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.
Donn Cave wrote:
> Since you apparently already know the PIDs you're looking for,
> it would be easier and more reliable to just kill them with 0 -
>
> Though of course that doesn't retrieve any of the other information
> you get from "ps".
>
> Donn Cave, http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
waiting for multiple child processes to finish Martin DeMello Ruby 1 11-13-2006 02:30 PM
Print digital photos online - 9 cents per print, plus 10% off. All sizes from 4x6 up to 30x60. Same price for Matte finish and glossy finish nathan_usny Digital Photography 2 09-12-2005 11:30 PM
Print digital photos online - 9 cents per print, plus 10% off. All sizes from 4x6 up to 30x60. Same price for Matte finish and glossy finish nathan_usny Digital Photography 0 09-12-2005 06:09 PM
Waiting for a site to finish loading Brett Javascript 3 05-28-2005 04:54 AM
waiting for external process to finish (using Runtime and Thread classes) brownjenkn@aol.com Java 1 04-22-2005 07:55 PM



Advertisments