python 获取进程的pid 和名字, 以及杀死进程

import psutilimport osdef get_all_pid_name():    pids = psutil.pids()    for pid in pids:        p = psutil.Process(pid)        print('pid-%s,pname-%s' % (pid, p.name()))def kill(pid):    # 本函数用于停止传入pid所对应的进程    if os.name == 'nt':        # Windows系统        cmd = 'taskkill /pid ' + str(pid) + ' /f'        try:            os.system(cmd)            print(pid, 'killed')        except Exception as e:            print(e)    elif os.name == 'posix':        # Linux系统        cmd = 'kill ' + str(pid)        try:            os.system(cmd)            print(pid, 'killed')        except Exception as e:            print(e)    else:        print('Undefined os.name')if __name__ == '__main__':    get_all_pid_name()    # kill(65408)
相关文章
相关标签/搜索