Python中的退出代码

问题:Python中的退出代码

我收到一条消息说script xyz.py returned exit code 0。这是什么意思?

Python中的退出代码是什么意思?那里有多少?哪些重要?

I got a message saying script xyz.py returned exit code 0. What does this mean?

What do the exit codes in Python mean? How many are there? Which ones are important?


回答 0

您在脚本中寻找的是对的调用sys.exit()。该方法的参数作为退出代码返回到环境。

该脚本很可能从未调用过exit方法,并且0是默认的退出代码。

What you’re looking for in the script is calls to sys.exit(). The argument to that method is returned to the environment as the exit code.

It’s fairly likely that the script is never calling the exit method, and that 0 is the default exit code.


回答 1

文档中sys.exit

可选参数arg可以是给出退出状态的整数(默认为零),也可以是其他类型的对象。如果它是整数,则shell等将零视为“成功终止”,而将任何非零值视为“异常终止”。大多数系统要求它的范围是0-127,否则会产生不确定的结果。某些系统具有为特定的退出代码分配特定含义的约定,但是这些通常不完善。Unix程序通常将2用于命令行语法错误,将1用于所有其他类型的错误。

在外壳程序脚本中使用退出代码的一个示例。在bash中,您可以检查特殊变量$?的最后退出状态:

me@mini:~$ python -c ""; echo $?
0
me@mini:~$ python -c "import sys; sys.exit(0)"; echo $?
0
me@mini:~$ python -c "import sys; sys.exit(43)"; echo $?
43

我个人尝试使用在/usr/include/asm-generic/errno.hLinux系统上找到的退出代码,但是我不知道这样做是否正确。

From the documentation for sys.exit:

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.

One example where exit codes are used are in shell scripts. In bash you can check the special variable $? for the last exit status:

me@mini:~$ python -c ""; echo $?
0
me@mini:~$ python -c "import sys; sys.exit(0)"; echo $?
0
me@mini:~$ python -c "import sys; sys.exit(43)"; echo $?
43

Personally I try to use the exit codes I find in /usr/include/asm-generic/errno.h (on a Linux system), but I don’t know if this is the right thing to do.


回答 2

作为记录,您可以使用此处定义的POSIX标准退出代码。

例:

import sys, os

try:
    config()
except:
    sys.exit(os.EX_CONFIG) 
try:
    do_stuff()
except:
    sys.exit(os.EX_SOFTWARE)
sys.exit(os.EX_OK) # code 0, all ok

For the record, you can use POSIX standard exit codes defined here.

Example:

import sys, os

try:
    config()
except:
    sys.exit(os.EX_CONFIG) 
try:
    do_stuff()
except:
    sys.exit(os.EX_SOFTWARE)
sys.exit(os.EX_OK) # code 0, all ok

回答 3

有一个errno模块定义标准退出代码:

例如,权限被拒绝是错误代码13

import errno, sys

if can_access_resource():
    do_something()
else:
    sys.exit(errno.EACCES)

There is an errno module that defines standard exit codes:

For example, Permission denied is error code 13:

import errno, sys

if can_access_resource():
    do_something()
else:
    sys.exit(errno.EACCES)

回答 4

退出代码0通常表示“这里没有问题”。但是,如果脚本的程序员未遵循约定,则可能必须查阅源代码以了解其含义。通常,将非零值作为错误代码返回。

Exit codes of 0 usually mean, “nothing wrong here.” However if the programmer of the script didn’t follow convention you may have to consult the source to see what it means. Usually a non-zero value is returned as an error code.


回答 5

答案是“取决于零退出码的含义”

但是,在大多数情况下,这意味着“一切都很好”


我喜欢POSIX:

因此,在外壳上,我将输入:

python script.py && echo 'OK' || echo 'Not OK'

如果我的python脚本调用,sys.exit(0)则外壳返回“ OK”

如果我的python脚本调用sys.exit(1)(或任何非零整数),则外壳程序返回“不正确”

了解外壳程序是您的工作,并阅读脚本的文档(或源代码)以了解退出代码的含义。

The answer is “Depends on what exit code zero means”

However, in most cases, this means “Everything is Ok”


I like POSIX:

So, on the shell, I would type:

python script.py && echo 'OK' || echo 'Not OK'

If my python script calls sys.exit(0) the shell returns ‘OK’

If my python script calls sys.exit(1) (or any non-zero integer), the shell returns ‘Not OK’

It’s your job to get clever with the shell, and read the documentation (or the source) for your script to see what the exit codes mean.


回答 6

操作系统命令具有退出代码。查找linux退出代码,以了解有关此内容的一些信息。外壳程序使用退出代码来确定程序是否正常运行,出现问题或失败。已经做了一些努力来创建标准(或至少是常用的)退出代码。请参阅此高级Shell脚本发布。

Operating system commands have exit codes. Look for linux exit codes to see some material on this. The shell uses the exit codes to decide if the program worked, had problems, or failed. There are some efforts to create standard (or at least commonly-used) exit codes. See this Advanced Shell Script posting.


回答 7

我建议使用内置的exit(0)进行干净关机,而不要使用sys.exit()

我不确定这是否是个好建议。

提到的文档非常如此

站点模块(在启动期间会自动导入,除非指定了-S命令行选项)会向内置命名空间添加多个常量。它们对于交互式解释器外壳很有用,不应在程序中使用

然后:

退出(代码=无)

对象,在打印时会打印一条消息,例如“使用quit()或Ctrl-D(即EOF)退出”,并在调用时使用指定的退出代码引发SystemExit。

如果将它与sys.exit()文档进行比较,则它使用的是引发SystemExit异常的相同机制。

I recommend a clean shutdown with the exit(0) builtin rather than using sys.exit()

I’m not sure if this is good advice.

The very documentation mentioned reads:

The site module (which is imported automatically during startup, except if the -S command-line option is given) adds several constants to the built-in namespace. They are useful for the interactive interpreter shell and should not be used in programs.

Then:

exit(code=None)

Objects that when printed, print a message like “Use quit() or Ctrl-D (i.e. EOF) to exit”, and when called, raise SystemExit with the specified exit code.

If we compare it to sys.exit() documentation, it’s using the same mechanism which is raising a SystemExit exception.


回答 8

如果你想移植应用的标准POSIX退出代码,请参阅exitstatusPyPI上的包。

安装软件包:

$ pip install exitstatus

在您的代码中使用:

import sys
from exitstatus import ExitStatus

sys.exit(ExitStatus.success)

If you would like to portably use the standard POSIX exit codes, see the exitstatus package on PyPI.

Install the package:

$ pip install exitstatus

Use in your code:

import sys
from exitstatus import ExitStatus

sys.exit(ExitStatus.success)

回答 9

退出代码仅具有脚本作者指定的含义。Unix的传统是退出代码0表示“成功”,其他都是失败的。确保给定脚本的退出代码意味着什么的唯一方法是检查脚本本身。

The exit codes only have meaning as assigned by the script author. The Unix tradition is that exit code 0 means ‘success’, anything else is failure. The only way to be sure what the exit codes for a given script mean is to examine the script itself.


回答 10

许多编程语言中的退出代码取决于程序员。因此,您必须查看程序源代码(或手册)。零通常表示“一切正常”。

Exit codes in many programming languages are up to programmers. So you have to look at your program source code (or manual). Zero usually means “everything went fine”.


回答 11

为了允许执行异常处理程序和其他操作,我建议使用 exit(0)内置而不要使用sys.exit()它来突然终止进程。

To allow exception handlers and other things execute I recommend a clean shutdown with the exit(0) builtin rather than using sys.exit() which terminates the process abruptly.