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)
相關文章
相關標籤/搜索