Linux中是否有任何标准的退出状态代码?

本文翻译自:Are there any standard exit status codes in Linux?

A process is considered to have completed correctly in Linux if its exit status was 0. 如果进程的退出状态为0,则认为该进程已在Linux中正确完成。

I've seen that segmentation faults often result in an exit status of 11, though I don't know if this is simply the convention where I work (the apps that failed like that have all been internal) or a standard. 我已经看到分段错误经常导致退出状态为11,但我不知道这是否只是我工作的惯例(那些失败的应用程序都是内部的)或标准。

Are there standard exit codes for processes in Linux? Linux中的进程是否有标准退出代码?


#1楼

参考:https://stackoom.com/question/4cfV/Linux中是否有任何标准的退出状态代码


#2楼

When Linux returns 0, it means success. 当Linux返回0时,表示成功。 Anything else means failure, each program has its own exit codes, so it would been quite long to list them all... ! 其他任何东西都意味着失败,每个程序都有自己的退出代码,所以列出它们的时间会很长......!

About the 11 error code, it's indeed the segmentation fault number, mostly meaning that the program accessed a memory location that was not assigned. 关于11错误代码,它确实是分段错误号,主要意味着程序访问了未分配的内存位置。


#3楼

'1' >>> Catchall for general errors '1' >>>捕获一般错误

'2' >>> Misuse of shell builtins (according to Bash documentation) '2' >>>滥用shell内置(根据Bash文档)

'126' >>> Command invoked cannot execute '126' >>>调用的命令无法执行

'127' >>>"command not found" '127' >>>“命令未找到”

'128' >>> Invalid argument to exit '128' >>>退出的参数无效

'128+n' >>>Fatal error signal "n" '128 + n' >>>致命错误信号“n”

'130' >>> Script terminated by Control-C '130' >>>脚本由Control-C终止

'255' >>>Exit status out of range '255' >>>退出状态超出范围

This is for bash. 这是为了bash。 However, for other applications, there are different exit codes. 但是,对于其他应用程序,有不同的退出代码。


#4楼

There are no standard exit codes, aside from 0 meaning success. 没有标准的退出代码,除了0意味着成功。 Non-zero doesn't necessarily mean failure either. 非零并不一定意味着失败。

stdlib.h does define EXIT_FAILURE as 1 and EXIT_SUCCESS as 0, but that's about it. stdlib.h确实将EXIT_FAILURE定义为1,将EXIT_SUCCESS定义为0,但这就是它。

The 11 on segfault is interesting, as 11 is the signal number that the kernel uses to kill the process in the event of a segfault. 关于段错误的11很有意思,因为11是内核在发生段错误时用来终止进程的信号号。 There is likely some mechanism, either in the kernel or in the shell, that translates that into the exit code. 在内核或shell中可能存在一些将其转换为退出代码的机制。


#5楼

Programs return a 16 bit exit code. 程序返回16位退出代码。 If the program was killed with a signal then the high order byte contains the signal used, otherwise the low order byte is the exit status returned by the programmer. 如果程序被信号杀死,则高位字节包含所使用的信号,否则低位字节是程序员返回的退出状态。

How that exit code is assigned to the status variable $? 如何将退出代码分配给状态变量$? is then up to the shell. 然后是shell。 Bash keeps the lower 7 bits of the status and then uses 128 + (signal nr) for indicating a signal. Bash保持状态的低7位,然后使用128 +(信号nr)来指示信号。

The only "standard" convention for programs is 0 for success, non-zero for error. 程序的唯一“标准”约定是0表示成功,非零表示错误。 Another convention used is to return errno on error. 使用的另一个约定是在出错时返回errno。


#6楼

sysexits.h has a list of standard exit codes. sysexits.h有一个标准退出代码列表。 It seems to date back to at least 1993 and some big projects like Postfix use it, so I imagine it's the way to go. 它似乎可以追溯到至少1993年和一些像Postfix这样的大项目使用它,所以我想它是要走的路。

From the OpenBSD man page: 从OpenBSD手册页:

According to style(9), it is not good practice to call exit(3) with arbi- trary values to indicate a failure condition when ending a program. 根据样式(9),在结束程序时,使用任意值调用exit(3)来指示失败条件并不是一个好习惯。 In- stead, the pre-defined exit codes from sysexits should be used, so the caller of the process can get a rough estimation about the failure class without looking up the source code. 相反,应该使用来自sysexits的预定义退出代码,因此进程的调用者可以在不查找源代码的情况下粗略估计失败类。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值