Python用 subprocess编写超时进程控制脚本
2014-08-18 22:26
267 查看
一直都寻找在Python下方便控制子进程运行时间的脚本。虽然网上有很多的好方法,但是都不能满足我的需求(也是我资质太低看别人的脚本总感觉太吃力,总有些看不明白的地方)。
下面这个脚本和网上一样利用了subprocess函数创建一个子进程控制脚本。(闲话少说,直接上菜!!!)
#!/usr/bin/python
import subprocess,time
def Test_ilo():
ilo_ip = '10.212.236.12'
sn_nu = 'BDSGJ11275067'
Returncode = "over"
cmd = 'sh ilo_test.sh %s %s' %(ilo_ip,sn_nu)
child = subprocess.Popen(cmd,stdout = subprocess.PIPE,shell = True)
timeout = 10
child_pid = child.pid
while True:
while_begin = time.time()
Flag = child.poll()
print Flag
if Flag == 0 and timeout > 0:
Returncode = child.stdout.read()
print Returncode
break
elif not Flag and timeout < 0:
child.kill()
print Returncode
break
else:
time.sleep(1)
if __name__ == '__main__':
print "Test_ilo", Test_ilo()
这里有个最致命的地方就是Flag的判断绝对不能写成Flag is ‘None’(兄弟我吃了不少亏啊)。
其中使用的一些函数就靠大家自己去找了,小弟也就不班门弄斧了。
下面这个脚本和网上一样利用了subprocess函数创建一个子进程控制脚本。(闲话少说,直接上菜!!!)
#!/usr/bin/python
import subprocess,time
def Test_ilo():
ilo_ip = '10.212.236.12'
sn_nu = 'BDSGJ11275067'
Returncode = "over"
cmd = 'sh ilo_test.sh %s %s' %(ilo_ip,sn_nu)
child = subprocess.Popen(cmd,stdout = subprocess.PIPE,shell = True)
timeout = 10
child_pid = child.pid
while True:
while_begin = time.time()
Flag = child.poll()
print Flag
if Flag == 0 and timeout > 0:
Returncode = child.stdout.read()
print Returncode
break
elif not Flag and timeout < 0:
child.kill()
print Returncode
break
else:
time.sleep(1)
if __name__ == '__main__':
print "Test_ilo", Test_ilo()
这里有个最致命的地方就是Flag的判断绝对不能写成Flag is ‘None’(兄弟我吃了不少亏啊)。
其中使用的一些函数就靠大家自己去找了,小弟也就不班门弄斧了。
相关文章推荐
- Python脚本控制的WebDriver 常用操作 <二十八> 超时设置和cookie操作
- 使用python语言编写脚本控制freeswitch总结
- perl脚本进程控制
- 转载:用Python编写自动下载网络小说的脚本
- 有关Python中线程的超时控制以及一个简单的应用
- Python 监控 DB 服务器信息(cpu, 磁盘,进程,alert log) 脚本
- Python 监控 DB 服务器信息(cpu, 磁盘,进程,alert log) 脚本
- 用python编写ASP脚本时遇到的问题,初步的解决方法,目前正在寻找更好的解决办法。
- Shell脚本学习笔记(八)--进程管理和工作控制
- 执行脚本实现超时时间控制 ---Process
- linux进程控制编程与守护进程编写实例
- Python 监控 DB 服务器信息(cpu, 磁盘,进程,alert log) 脚本
- 进程服务编写,与启动停止控制
- 用python编写脚本从wireshark导出的数据文件中提取数据
- 自己编写的一个查找Linux进程的Shell脚本
- 使用Bash编写Linux Shell脚本-8.调试和版本控制
- 自动查杀超时或僵死的进程的脚本(zt)
- 用Python编写自动下载网络小说的脚本
- python类库31[进程subprocess]
- Shell脚本学习笔记(八)--进程管理和工作控制