写此脚本的起因:
运行了好多python脚本,每次想要停下来的时候,需要一个一个kill掉,十分不便利
run_task_1 2 3,预计会更多,需要一个简单的办法kill掉某一个,或是这是全部kill掉,于是别写了下面的这个脚本
#coding=utf-8
import sys,os
def kill_crawler(id):
cmd = 'ps -ef | grep python'
f = os.popen(cmd)
txt = f.readlines()
for line in txt:
colum = line.split()
pid = colum[1]
name = colum[-1]
if name.startswith('run_task_'):
task_id = name[9:-3]
if task_id == id or id =='0':
cmd = "kill -9 %d" % int(pid)
rc = os.system(cmd)
if rc == 0 :
print "stop \"%s\" success!!" % name
else:
print "stop \"%s\" failed!!" % name
if __name__ =='__main__':
if not len(sys.argv)==2:
print u'输入要结束的任务编号,0代表停止所有'
sys.exit()
id = sys.argv[1]
kill_crawler(id)
#!/bin/sh
nohup python run_task_1.py &
nohup python run_task_2.py &
nohup python run_task_3.py &