Skip to content

Instantly share code, notes, and snippets.

@mebusw
Last active December 7, 2016 14:47
Show Gist options
  • Save mebusw/5c05dbd01c0b3e6aaf863eba64be5208 to your computer and use it in GitHub Desktop.
Save mebusw/5c05dbd01c0b3e6aaf863eba64be5208 to your computer and use it in GitHub Desktop.
#! /usr/local/bin/python
# -*- coding: utf-8 -*-
import subprocess, time
# 程序执行终止时间为当前时刻延迟N秒
stoptime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()+60))
def main(command):
popen = subprocess.Popen(
command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
pid = popen.pid
print('Popen.pid:'+str(pid))
while True:
#time.sleep(1)
line = popen.stdout.readline().strip()
print line
#print(popen, subprocess.Popen.poll(popen))
# 当前时间
thistime = time.strftime(
'%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
try:
subprocess.Popen.poll(popen)
except:
break
if thistime >= stoptime:
try:
# 终止子进程
popen.terminate()
# 等待子进程终止后跳出while循环
if subprocess.Popen.poll(popen) is not None:
break
else:
print('waiting for subprocess.Popen terminate()')
except:
print('subprocess already done')
break
print('DONE')
if __name__ == '__main__':
main('mvn test')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment