Waiting for processes to finish under Solaris

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Behrang Dadsetan

    #1

    Waiting for processes to finish under Solaris

    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(s ys.argv[1:], "hr:p:", ["help", "regexp=",
    "pid="])
    except getopt.GetoptEr ror:
    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.readline s()
    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(lin e):
    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(c ommand)[1]
    for watchedpid in pids:
    if watchedpid == pid:
    watchedforpids[pid] = command

    for watchedforcmd in regexps:
    if watchedforcmd.s earch(command) or
    watchedforcmd.s earch(basenamec md):
    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)
    ------------------------------------------------------------------------------

  • Behrang Dadsetan

    #2
    Re: Waiting for processes to finish under Solaris

    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:[color=blue]
    > 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, donn@u.washingt on.edu[/color]

    Comment

    Working...