用python杀死进程



我试图杀死最近启动的进程。在我的程序启动后,在一个条件之后,程序将继续运行,但它只杀死了在程序运行之前启动的进程,如果我先运行它,然后打开一些什么都没发生的东西,有人能帮我吗?我试了好几件事,结果都在同一个地方,非常感谢。(OS=LINUX((抱歉英语不好,我还在学习(

p = psutil.pids()
for x in p:
s = psutil.Process(x)
tempo_processos = datetime.datetime.fromtimestamp(s.create_time()).strftime("%H%M%S")
if int(time.strftime("%H%M%S")) - int(tempo_processos) < 500:  #get the current time taking the time that the process was started to get the shortest possible time
if s.name() == 'python3':
pass
else:
os.popen(f"kill -9 {x}")


psutil.pids获取代码执行时运行的进程的PID,因此在代码启动后启动的任何进程都不会被psutil.pids返回,因此不能被杀死。

你可以试试这样的东西:

while True:
p = psutil.pids()
kill processes
wait(some time)

最新更新