如何使用通过模块 sys 运行的命令让 Python 程序杀死自己?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29065781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get a Python program to kill itself using a command run through the module sys?
提问by d3pd
I want to see how a Python program could kill itself by issuing a command using the module sys
. How could I get it to kill itself using a command of the following form?:
我想看看 Python 程序如何通过使用模块发出命令来杀死自己sys
。我怎样才能使用以下形式的命令杀死它自己?:
os.system(killCommand)
EDIT: emphasising for clarity
编辑:强调清晰
So, just to be clear, I want to have a stringthat gets run in the shell that kills the Python program.
所以,为了清楚起见,我想要一个在 shell 中运行的字符串来杀死 Python 程序。
采纳答案by Antti Haapala
You can use sys.exit()
to exit the program normally.
您可以使用sys.exit()
来正常退出程序。
Exit the interpreter by raising
SystemExit(status)
. If the status is omitted orNone
, it defaults to zero (i.e., success). If the status is an integer, it will be used as the system exit status. If it is another kind of object, it will be printed and the system exit status will be one (i.e., failure).
通过提高退出解释器
SystemExit(status)
。如果省略状态或None
,则默认为零(即成功)。如果状态是整数,它将用作系统退出状态。如果是另一种对象,则打印出来,系统退出状态为1(即失败)。
The system command to kill the interpreter itself depends on the shellused; if your shell is bash
or zsh
, you can use:
杀死解释器本身的系统命令取决于所使用的shell;如果您的外壳是bash
或zsh
,则可以使用:
a@host:~$ python
Python 2.7.8 (default, Oct 20 2014, 15:05:19)
[GCC 4.9.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.system('kill $PPID')
Terminated
a@host:~$
Though your actual results may vary. To be safer, you need to provide the process ID yourself:
尽管您的实际结果可能会有所不同。为了更安全,您需要自己提供进程ID:
>>> os.system('kill %d' % os.getpid())
If you want to just a get signal sent to your process, you can also use os.kill()
with the process id of your process; the process id of currently running process is available from os.getpid()
:
如果您只想将 get 信号发送到您的进程,您还可以使用os.kill()
您的进程的进程 ID;当前正在运行的进程的进程 ID 可从os.getpid()
:
a@host:~$ python
Python 2.7.8 (default, Oct 20 2014, 15:05:19)
[GCC 4.9.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.kill(os.getpid(), 9)
[1] 27248 killed python
a@host:~$?
回答by mbatchkarov
sys.exit(1)
will terminate the current program. The parameter is the exit status- anything but 0 indicates abnormal termination.
sys.exit(1)
将终止当前程序。该参数是退出状态——除 0 外的任何内容都表示异常终止。
回答by Kasramvd
Actually its what that sys.exit
is for :
实际上它的sys.exit
用途是:
Exit from Python. This is implemented by raising the SystemExitexception, so cleanup actions specified by finally clauses of try statements are honored, and it is possible to intercept the exit attempt at an outer level.
The optional argument arg can be an integer giving the exit status (defaulting to zero), or another type of object. If it is an integer, zero is considered “successful termination” and any nonzero value is considered “abnormal termination” by shells and the like. Most systems require it to be in the range 0-127, and produce undefined results otherwise. Some systems have a convention for assigning specific meanings to specific exit codes, but these are generally underdeveloped; Unix programs generally use 2 for command line syntax errors and 1 for all other kind of errors. If another type of object is passed, None is equivalent to passing zero, and any other object is printed to stderr and results in an exit code of 1. In particular, sys.exit("some error message") is a quick way to exit a program when an error occurs.
Since exit() ultimately “only” raises an exception, it will only exit the process when called from the main thread, and the exception is not intercepted.
退出 Python。这是通过引发SystemExit异常来实现的,因此执行 try 语句的 finally 子句指定的清理操作,并且可以在外部级别拦截退出尝试。
可选参数 arg 可以是给出退出状态的整数(默认为零)或其他类型的对象。如果它是一个整数,零被认为是“成功终止”,任何非零值都被壳等认为是“异常终止”。大多数系统要求它在 0-127 的范围内,否则会产生未定义的结果。一些系统有为特定退出代码分配特定含义的约定,但这些通常不完善;Unix 程序通常使用 2 表示命令行语法错误,使用 1 表示所有其他类型的错误。如果传递了另一种类型的对象,则 None 等效于传递零,并且任何其他对象都将打印到 stderr 并导致退出代码为 1。特别是, sys.exit("some error message") 是一种快速的方法发生错误时退出程序。
由于exit()最终“只”抛出异常,所以只有从主线程调用时才会退出进程,不会拦截异常。
回答by iron coder
If you are in the main process (i.e. Your mail python script) you can use os
module
如果您在主进程(即您的邮件 python 脚本)中,您可以使用os
模块
import os
import signal
#Your Python code
os.kill(os.getpid(),signal.SIGKILL)