【Linux】python中 查看linux系统进程pid, 并kill掉

代码:

点击查看代码
import os
import sys
import subprocess

def get_process_id(name):
    child = subprocess.Popen(["pgrep", "-f", name], stdout=subprocess.PIPE, shell=False)
    response = child.communicate()[0]
    return [int(pid) for pid in response.split()]

pids = get_process_id("ftp_gfs.py")
    if not pids:
        print("no target pid to kill,please check")
        # sys.exit(1)
    else:
        for pid in pids:
            print(pid)
            result = os.system("kill -9 " + str(pid))
            if result == 0:
                print(f'{pid} kill success')
posted @ 2022-01-20 16:07  是阿杰呀  阅读(467)  评论(0)    收藏  举报