Ubuntu环境下经常能遇到清除pid的工作,后面可以试试我这个小工具啦~
输入一个进程名称或关键字,展示与输入相关的进程信息,按需杀死进程
废话不多说,直接上才艺
import os
def find_pid(process_name):
grep_ps_cmd = "ps -ef | grep {} | grep -v grep".format(process_name)
process_res = os.popen(grep_ps_cmd).read()
for pr in process_res.split("\n"):
print(pr)
def kill_pid(pid):
kill_cmd = "kill -9 {}".format(pid)
os.system(kill_cmd)
check_pid_is_kill(pid)
def check_pid_is_kill(pid):
recheck_res = os.popen("ps -ef | grep " + pid + " | grep -v grep").read()
if recheck_res == "":
print("进程清除成功")
else:
print("进程清除失败,尝试使用sudo~")
def kill_by_ps(ps):
grep_ps_cmd = "ps -ef | grep " + ps + " | grep -v grep | awk '{print $2}'"
process_res = os.popen(grep_ps_cmd).read()
for pr in process_res.split("\n"):
print("将要清除的进程号是:", process_res)
kill_pid(pr)
def menu():
print("*" * 10 + "Menu" + "*" * 10 + "\n")
print("1. 查看进程信息(可以输入进程名称或进程号)\n")
print("2. 根据pid杀死进程\n")
print("3. 根据进程关键字杀死全部进程(慎用可能会误清)\n")
print("按下 Ctrl+C 退出\n")
if __name__ == '__main__':
while True:
menu()
case = input("请输入你要进行的操作代号: \n").strip()
if case == "1":
ps_name = input("请输入进程名称或pid:\n")
find_pid(ps_name.strip())
elif case == "2":
pid_num = input("请输入进程号:\n").strip()
kill_pid(pid_num)
elif case == "3":
ps_name = input("请输入进程名称:\n")
kill_by_ps(ps_name)
菜单:

3513

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



