实验室中一段监听某程序结束后自动重启该改程序的Python脚本

实验室中一段监听某程序结束后自动重启该改程序的Python脚本

#!/usr/bin/env python
#coding=utf-8
import subprocess 
import sys
import os
import time

def changeWBAccount(iPos):
#一段处理获得登录信息的函数
	# ...
	return userName, userPwd

if __name__ == '__main__' :
	userName, userPwd = changeWBAccount(1)
	programPath = sys.path[0] + '/func_start.py'
	prog = subprocess.Popen(['python', programPath, userName, userPwd], shell=False, stdout=False)
	print 'Current PID: ', prog.pid
	#注意!如果传递带参数的命令,则需要在Popen中的列表内填写“参数”,
	#例如本例传递命令是: python /root/test/func_start.py Tester 123456
	
	while (True): 
		time.sleep(1)
		retCode = subprocess.Popen.poll(prog) 
		if retCode is not None:
			print 'Process will reset ...'
			programPath = sys.path[0] + '/func_start.py'
			prog = subprocess.Popen(['python', programPath, userName, userPwd], shell=False, stdout=False)
			print 'Current PID: ', prog.pid


你可能感兴趣的:(Linux,Python)