1.可执行程序
os.system('pgrep %s > %s' % (process, output))
pidfile = open("output", 'r')
totalpid = len(pidfile.readlines())
pidfile.close()
if totalpid == 0 :
#没有进程
return False
elif totalpid > 1 :
#多个进程
os.system('killall -9 %s' % (process))
return False
else :
return True
2.python 启动的脚本
os.system('ps aux | grep %s > %s' % (process, output))
pidfile = open(output, 'r')
totalpid = len(pidfile.readlines())
pidfile.close()
while totalpid > 3 :
os.system("kill -9 `ps aux | grep %s | sed -n '1P' | awk '{print $2}' ` " % (process))
os.system('ps aux | grep %s > %s' % (process, output))
pidfile = open(output, 'r')
totalpid = len(pidfile.readlines())
pidfile.close()
转载于
本文介绍如何使用Python通过os.system命令来管理和杀死进程。包括两种情况:一是管理常规可执行程序的进程,二是管理由Python启动的脚本进程。通过示例代码展示了如何查找进程PID、判断进程是否存在以及如何终止进程。
6392

被折叠的 条评论
为什么被折叠?



