歡迎您光臨本站 註冊首頁

Python實現Linux下面按名字kill掉進程

←手機掃碼閱讀     火星人 @ 2014-03-26 , reply:0

Linux下面按進程的命令名字kill掉進程稍稍有點麻煩,就用python寫了一個簡單的小工具,不過目前這個工具還有點小問題,就是隨便輸入一個名字,執行ps aux|grep %name 命令,本身也產生一個進程,但是這個進程隨即消失,以後kill的時候就找不到這個進程了。不過運行結果還是滿意的。

xkill
#!/usr/bin/python

import os,re,sys

def kill_by_name(name):
cmd='ps aux|grep %s'%name
f=os.popen(cmd)
regex=re.compile(r'\w+\s+(\d+)\s+.*')
txt=f.read()
if len(txt)<5:
print 'there is no thread by name or command %s'%name
return

ids=regex.findall(txt)
cmd="kill %s"%' '.join(ids)
os.system(cmd)


if __name__=='__main__':
if len(sys.argv)==1:
name=raw_input("type the process command name:")
else:
name=sys.argv[1]
kill_by_name(name)

[火星人 ] Python實現Linux下面按名字kill掉進程已經有464次圍觀

http://coctec.com/docs/linux/show-post-188176.html