应用场景:
linux上经常有一些定时任务的脚本,而且 有些不允许多个同时 运行,必须等上一个结束。
但有时候,会有进程假死。
def isRunning(process_name):
try:
process = len(os.popen('ps aux | grep "' + process_name + '" | grep -v grep').readlines())
if process > 1:
check = os.popen("ps -eo pid,etime,cmd | grep "+process_name+" | grep -v grep | awk '{print $2}' | sed \"s/-/:/g\" | awk -F: '{print $1*43200+$2*3600+$3*60+$4}'").readlines()[1]
if check > 300 :
print '当前进程已经运行超过300秒,即将主动结束'
ps.popen('ps -ef | grep '+process_name+' | grep -v grep | cut -c 9-15 | xargs kill 9')
os.sleep(1)
return 0
return 1
else:
return 0
except:
# print("Check process ERROR!!!")
return 0
感谢 茅草屋 提供技术帮助 解决时间计算问题
标签: 进程监控
非特殊说明,本博所有文章均为博主原创。
如若转载,请注明出处:https://www.isres.com/linux/183.html