Thursday, July 8, 2010

Standard exit codes - shell scripts versus binaries

This short article is prompted by the question "What return codes should I use in my shell script?"

Some Answers:
  • A non-zero value
  • A non-zero value in the ranges 1 through 127 and 138 through 255
  • A non-zero value in the ranges 1 through 63, 79 through 127, and 138 through 255
The first answer is certainly correct.

The remaining answers are really a matter of personal preference.


Exit codes 128 through 137:

If a process is terminated by a signal then the standard behaviour is to take the numeric value of the signal, add 128, and use that value as exit code.

128+SIGNAL

So kill -9 someprocess should in theory see the process exit with code 137

There are more than 9 signal codes so you could, if you wish, avoid some of the codes 138 onwards to be absolutely sure.

( Avoiding 128 through 159 might be your preference )

The signal codes for Linux are described here.


Exit codes 64 through 78:

From the Linux exit manpage at kernel.org:
BSD has attempted to standardize exit codes; see the file <sysexits.h>
The actual meanings of the codes are given in this OpenBSD page, but I reproduce the first (64) and last (78) directly give you a flavour:

  • EX_USAGE (64) The command was used incorrectly, e.g., with the wrong number of arguments, a bad flag, a bad syntax in a parameter, or whatever.
  • EX_CONFIG (78) Something was found in an unconfigured or misconfigured state.

Shell script return codes - my personal suggestion:

Have a quick look through the OpenBSD range 64 to 78 and find something suitable. Then add 100 to that code.

First Example (code 175):

75 in OpenBSD says:
Temporary failure, indicating something that is not really an error. In sendmail, this means that a mailer (e.g.) could not create a connection, and the request should be reattempted later.

( Now adding 100 give 175 which I use )


Second Example (code 178):

78 in OpenBSD says:
Something was found in an unconfigured or misconfigured state.
( Now adding 100 gives 178 which I use )


The bash scripting guide notes (tldp.org):

Appendix D gives some guidance about exit codes.


Linux documentation of sysexits.h (permission of BSD):

#define EX__BASE 64 /* base value for error messages */
#define EX_USAGE 64 /* command line usage error */
#define EX_DATAERR 65 /* data format error */
#define EX_NOINPUT 66 /* cannot open input */
#define EX_NOUSER 67 /* addressee unknown */
#define EX_NOHOST 68 /* host name unknown */
#define EX_UNAVAILABLE 69 /* service unavailable */
#define EX_SOFTWARE 70 /* internal software error */
#define EX_OSERR 71 /* system error (e.g., can't fork) */
#define EX_OSFILE 72 /* critical OS file missing */
#define EX_CANTCREAT 73 /* can't create (user) output file */
#define EX_IOERR 74 /* input/output error */
#define EX_TEMPFAIL 75 /* temp failure; user is invited to retry */
#define EX_PROTOCOL 76 /* remote error in protocol */
#define EX_NOPERM 77 /* permission denied */
#define EX_CONFIG 78 /* configuration error */
#define EX__MAX 78 /* maximum listed value */ 
 
 



If you have the Linux source installed then the file /usr/include/sysexits.h contains the text pasted above.

No comments:

Post a Comment

comment