简体   繁体   English

如何在Windows上的Python中杀死script.py?

[英]How to kill script.py in Python on Windows?

Is it possible in Python to close a script using just the filename of the script? 在Python中是否可以仅使用脚本文件名来关闭脚本? I have two scripts which don't work at the same time so on startup I want one of the scripts to end the other script. 我有两个不能同时工作的脚本,因此在启动时我希望其中一个脚本结束另一个脚本。 Is this possible? 这可能吗? If so, please let me know how. 如果是这样,请让我知道如何。

Any help greatly appreciated. 任何帮助,不胜感激。 Thanks, 谢谢,

Mitra0000 米特拉0000

In this answer , it says that you can. 在这个答案中 ,它说可以。 You would need a "PID (Process Identifier)" and know whether you are using Unix or Python. 您将需要一个“ PID(进程标识符)”,并且知道您使用的是Unix还是Python。 Why? 为什么? Because there are two different ways: 因为有两种不同的方式:

Unix: Unix:

import os, signal
os.kill(5383, signal.SIGKILL)

Windows: 视窗:

import subprocess as s
def killProcess(pid):
    s.Popen('taskkill /F /PID {0}'.format(pid), shell=True)

The answer also mentioned: 答案还提到:

You can send the PID to the other programm or you could search in the process-list to find the name of the other script and kill it with the above script. 您可以将PID发送到其他程序,也可以在进程列表中搜索以找到其他脚本的名称,并使用上述脚本将其杀死。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM