This script was not intended to be used from the command line, but to be called from elsewhere. It was only a test piece of code.
If you use the ping command with the args that you are calling it with, there is no need to kill the process, it will terminate after 10 requests.  8-)

Johan

Christopher Arndt wrote:
Alan Gauld schrieb:
  
Thanks, I may use that as one of the example programs if
you don't mind?
    

I took the liberty of refactoring Johan's example a bit, to make it more
reusable. See attached file.

Chris
  

"""Wrapper object for external commands, that allows to kill them after later.. ::Author: Johan Geldenhuys [EMAIL PROTECTED] ::Version: 0.0.2 ::Date last updated: 2005-11-16 ::Changes: - refactored by Christopher Arndt :: TODO: Capture the output from line 41 to a file. """ import os, signal, time class KillableCommand(object): def __init__(self, command, *args): self.pid = None self.command = command self.args = args def kill(self, signal=signal.SIGTERM): try: os.kill(self.pid, signal) except: raise OSError, "Could not kill process." def run(self, *args): self.pid = os.fork() args = list(self.args + args) if self.pid == 0: os.execvp(self.command, [self.command] + args) if __name__ == '__main__': cmdname = "ping" print "Starting", cmdname #cmd = KillableCommand('tcpdump', '-npi') cmd = KillableCommand(cmdname, '-c', '10') cmd.run('www.python.org') print "PID: ", cmd.pid print "Letting it run for 5 seconds..." time.sleep(5) try: print "Trying to kill pid %d..." % cmd.pid cmd.kill() except OSError, e: print e else: print "Killed pid %d." % cmd.pid

_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to