os.system easy question

Donn Cave donn at u.washington.edu
Tue Jan 23 12:00:46 EST 2001


Quoth Erno Kuusela <erno-news at erno.iki.fi>:
| In article <slrn96qrgp.gg.huwdotlynes at Marvin.ic.ac.uk>,
| huwdotlynes at ic.ac.uk (Huw Lynes) writes:
|
|> how to kill the 
|> external program after it has run. Or alternatively how to
|> run an external program in such a way that the script will
|> not halt.
|
| do something like...
|
| import signal, os
|
| class BgProc:
|     def __init__(self, cmdline):
|         pid = os.fork()
|         if pid > 0:
|             self.pid = pid
|         else:
|             try:
|                 os.execvp('/bin/sh', ['sh', '-c', cmdline])
|             finally:
|                 os._exit(255)
|
|     def wait(self):
|         return os.waitpid(self.pid, 0)
|
|     def kill(self, sig = signal.SIGTERM):
|         os.kill(self.pid, sig)
|         return self.wait()

That ought to work, just want to mention that in Python 2.0 the
fork & exec stuff can be turned over to os.spawnv() if you like:

   pid = os.spawnv(os.P_NOWAIT, '/bin/sh', ('sh', '-c', cmd))

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list