import logging
import os, signal
logger = logging.getLogger("")
program = 'de_dump.py'
out = os.popen(f"ps aux | grep {program}").read()
for line in out.splitlines():
print(line)
if program in line:
pid = int(line.split()[1])
try:
os.kill(pid, signal.SIGKILL)
print(f'已杀死pid为{pid}的进程')
except OSError:
print('没有如此进程!!!')
import psutil
def kill_process_by_name(process_name):
pid_list = psutil.pids()
for pid in pid_list:
try:
p = psutil.Process(pid)
cmd = p.cmdline()
if process_name in cmd:
print(cmd)
p.terminate()
except Exception:
pass
if __name__ == '__main__':
kill_process_by_name('b.py')
网友评论