如何通过以相同脚本运行的方式销毁.exe文件(未从py转换) [英] How to destroy an .exe file(not converted from py) by run as the same script

查看:101
本文介绍了如何通过以相同脚本运行的方式销毁.exe文件(未从py转换)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,该脚本通过subprocess.Popen()运行一个.exe文件,但是我刚刚意识到,即使关闭脚本,该.exe文件也可以继续运行.有什么办法可以通过文件名或ID停止运行.exe文件?该exe文件不是从py文件转换而来的,所以我被卡住了.

I have a script that runs an .exe file via subprocess.Popen(), but I just realized, that .exe file keeps running even I close my script. Is there any way to stop running an .exe file via name of it or ID? That exe file is not converted from a py file, so I'm stuck.

推荐答案

如果运行process = subprocess.Popen(...),则可以稍后使用process.terminate()终止该过程.

If you run process = subprocess.Popen(...) then you could terminate the process later using process.terminate().

如果子进程可以创建自己的子进程,则process.terminate()和/或process.kill()可能不够.在Windows上,您可能需要创建一个 Job对象,并将.exe进程分配给它.在Unix上,您可以使用start_new_session=True(在较早的Python版本上为preexec_fn=os.setsidpreexec_fn=os.setpgrp)加os.pgkill(process.pid, signal.SIGINT)杀死属于同一进程组的进程(在Linux上为prctl(PR_SET_PDEATHSIG, ...)),以在父进程发送信号死亡).参见

If the subprocess may create its own children then process.terminate() and/or process.kill() might not be enough. On Windows, you might need to create a Job object and assign the .exe process to it. On Unix, you could use start_new_session=True (preexec_fn=os.setsid or preexec_fn=os.setpgrp on earlier Python versions) plus os.pgkill(process.pid, signal.SIGINT) to kill processes belonging to the same process group (or prctl(PR_SET_PDEATHSIG, ...) on Linux, to send a signal when the parent dies). See

  • How to terminate a python subprocess launched with shell=True
  • Python: how to kill child process(es) when parent dies?

另请参见, killableprocess.py

See also, killableprocess.py

这篇关于如何通过以相同脚本运行的方式销毁.exe文件(未从py转换)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆