- child = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
- child.wait() # wait for proc to hang up
- if child.returncode:
- raise Exception("command failed:\n stdout - %s\n stderr - %s" % \
- (child.stdout.readlines(), child.stderr.readlines()))
- except: log_exc('failed to run command %s' % ' '.join(args))
+ child = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
+ while True:
+ # see if anything can be read within the poll interval
+ (r,w,x)=select.select([child.stdout,child.stderr],[],[],poll)
+ # read and log it
+ for fd in r:
+ input=fd.read()
+ if input: log(input)
+ # is process over ?
+ returncode=child.poll()
+ # yes
+ if returncode != None:
+ # child is done and return 0
+ if returncode == 0:
+ verbose('log_call: command completed %s' % ' '.join(command))
+ break
+ # child has failed
+ else:
+ raise Exception("log_call: failed with returncode %d"%returncode)
+ # no : still within timeout ?
+ if time.time() >= trigger:
+ child.terminate()
+ raise Exception("log_call: terminated command - exceeded timeout %d s"%timeout)
+ except: log_exc('failed to run command %s' % ' '.join(command))