#!/usr/bin/env python import os import sys import subprocess import time import signal import guestfs class ProcessMonitor(object): def __init__(self, command): self.p = subprocess.Popen(command) self.executible = command[0] self.rc = None def __del__(self): self.stop() def is_running(self): if self.rc != None: return False self.rc = self.p.poll() if self.rc != None: return False else: return True def stop(self): if self.is_running(): os.kill(self.p.pid, signal.SIGTERM) self.rc = self.p.wait() def retcode_get(self): return self.rc def __str__(self): if self.is_running(): return '%s(%d) is running' % (self.executible, self.p.pid) else: return '%s(%d) exited with %d' % (self.executible, self.p.pid, self.rc) if __name__ == '__main__': qpidd = ProcessMonitor(['qpidd', '-p', '49000', '--auth', 'no']) print qpidd iso = '%s/Downloads/iso-images/gnome3_test_20110310_x86_64.iso' % os.environ['HOME'] g = guestfs.GuestFS() g.add_drive_opts(iso, format='raw') g.launch() time.sleep(1) g.umount_all() g.kill_subprocess() del g qpidd.stop() print qpidd