473,503 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Jul 18 '05 #1
1 2631
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, do**@u.washington.edu


Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
7031
by: Bob Swerdlow | last post by:
My application starts up a number of processes for various purposes using: self.popen = popen2.Popen3("/usr/local/bin/python -O "myscript.py") and then shuts them down when appropriate with...
2
1502
by: Rech | last post by:
Hi, I need a little help here managing child processes in Python. I'm not so skilled in system programming so I hope you can give me some good suggestions. I have a very CPU and memory...
39
789
by: jabailo | last post by:
I am looping through a text file, and with each row, I launch a web service, asynchronously. Before I move on to the next step in the process, I want to make sure that all the web services have...
2
1783
by: murray_shane56 | last post by:
We currently have a routine that "forks" out (to use the unix term)TSQL commands to run asynchronously via SQL Agent jobs. Each TSQL command gets its own Job, and the job starts immediately after...
6
7630
by: Hugh Janus | last post by:
Hi group, I am executing a shell command that calls a .BAT file. I have to wait for this batch file to finish so I use the wait = true parameter in the shell command. However, this means that...
3
2909
by: s99999999s2003 | last post by:
hi i need to go into a directory to grab some files and do some processing. The thing is, i need to wait till the process that generates the files in that directory to finish before i can grab...
9
5156
by: Tony | last post by:
Hi all, I have a DB2 8.2(fk14) running on Solaris 9 server. Once the DB2 working, I can see more than 30 DB2 processes(db2sysc) running at the same time. Is that normal? Thanks.
3
5149
by: =?ISO-8859-1?B?Rvpsdmlv?= | last post by:
Hello all, My application delay some minutes when I press a button(it's doing some working ). How can I put a message: "Please wating" during this delay? It is important that after the...
1
2754
by: Simon_21 | last post by:
Hi All, I have a servlet which is invoked from a jsp page. While the serlvet is executing, the jsp page is waiting for the servlet to complete. When the servlet completes, it informs the waiting...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5022
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4688
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
399
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.