[Python] 특정 command psutil로 사용하여 pid 종료하기

2020. 9. 14. 18:39분석 Python/구현 및 자료

728x90

여기서는 command에 파이썬이면서 joblib이 있는 것들 킬하기

import psutil , re
results = []
for proc in psutil.process_iter(['pid', 'name', 'username',"cmdline"]):
    if (proc.info["cmdline"] != []) & (proc.info["name"] == "python") :
        bool_sum = np.sum([True for i in proc.info["cmdline"] if re.search("joblib" , i) is not None])
        if bool_sum > 0 :
            results.append(proc.info["pid"])
for pid in results :            
    psutil.Process(pid).kill()

#kill $(ps aux | grep '[p]ython csp_build.py' | awk '{print $2}')
728x90