I keep hearing a voice saying, "UR doing it r0ng!" I have a python
program that is running shell commands. I'm using the subprocess
library. Some of the shell commands take a long time. When I kill
the python process (SIGTERM), the subprocess keeps on going. I want
it to die too. So I registered a SIGTERM handler to manually
terminate the subprocess, but I still get some ugliness. Here's a
boiled down example of what I'm attempting:
#! /usr/bin/env python
# subproc.py
import subprocess
from signal import signal, SIGTERM
proc = None
def cleanup(signum, frame):
print "cleaning up"
if proc:
proc.terminate()
command = 'while [ 1 ]; do echo "still running"; sleep 1; done'
print "running command: " + command
signal(SIGTERM, cleanup)
proc = subprocess.Popen(command, shell=True)
proc.communicate()
---
Without the signal handler, you can send a SIGTERM to subproc.py and
it dies, but the while loop lives on. With the signal handler,
SIGTERM kills them both, but the final output looks like this:
cleaning up
Traceback (most recent call last):
File "./subproc.py", line 17, in <module>
proc.communicate()
File "/usr/lib/python2.6/subprocess.py", line 686, in communicate
self.wait()
File "/usr/lib/python2.6/subprocess.py", line 1157, in wait
pid, sts = os.waitpid(self.pid, 0)
OSError: [Errno 4] Interrupted system call
It just seems like there should be a cleaner, easier way to do this.
Bryan
--------------------
BYU Unix Users Group
http://uug.byu.edu/
The opinions expressed in this message are the responsibility of their
author. They are not endorsed by BYU, the BYU CS Department or BYU-UUG.
___________________________________________________________________
List Info (unsubscribe here): http://uug.byu.edu/mailman/listinfo/uug-list