【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')

浙公网安备 33010602011771号