<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
<title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
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. <br>
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. <span class="moz-smiley-s11"><span> 8-) </span></span><br>
<br>
Johan<br>
<br>
Christopher Arndt wrote:
<blockquote cite="mid437BAD36.3090007@web.de" type="cite">
<pre wrap="">Alan Gauld schrieb:
</pre>
<blockquote type="cite">
<pre wrap="">Thanks, I may use that as one of the example programs if
you don't mind?
</pre>
</blockquote>
<pre wrap=""><!---->
I took the liberty of refactoring Johan's example a bit, to make it more
reusable. See attached file.
Chris
</pre>
<pre wrap="">
<hr size="4" width="90%">
"""Wrapper object for external commands, that allows to kill them after later..
::Author: Johan Geldenhuys
<a class="moz-txt-link-abbreviated" href="mailto:johan@accesstel.co.za">johan@accesstel.co.za</a>
::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('<a class="moz-txt-link-abbreviated" href="http://www.python.org">www.python.org</a>')
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
</pre>
<pre wrap="">
<hr size="4" width="90%">
_______________________________________________
Tutor maillist - <a class="moz-txt-link-abbreviated" href="mailto:Tutor@python.org">Tutor@python.org</a>
<a class="moz-txt-link-freetext" href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a>
</pre>
</blockquote>
</body>
</html>