Waiting for processes to finish under Solaris

Discussion in 'Python' started by Behrang Dadsetan, Jul 15, 2003.

  1. 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)
    ------------------------------------------------------------------------------
     
    Behrang Dadsetan, Jul 15, 2003
    #1
    1. Advertising

  2. 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,
     
    Behrang Dadsetan, Jul 15, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    14,246
    Matt Humphrey
    Apr 22, 2005
  2. Martin DeMello
    Replies:
    1
    Views:
    209
    Farrel Lifson
    Nov 13, 2006
  3. Brett

    Waiting for a site to finish loading

    Brett, May 28, 2005, in forum: Javascript
    Replies:
    3
    Views:
    130
    Randy Webb
    May 28, 2005
  4. matej
    Replies:
    1
    Views:
    126
    Bjoern Hoehrmann
    Jun 25, 2008
  5. Roedy Green
    Replies:
    31
    Views:
    629
    Robert Klemme
    Jan 26, 2013
Loading...

Share This Page