Exit Codes With Special Meanings

Exit codes for Unix programs and scripts are defined in /usr/include/sysexits.h
on BSD systems but the codes are standardized across most Unix platforms.

"Reserved" Exit Codes

1     - Catchall for general errors (let "var1 = 1/0")
        Miscellaneous errors, such as "divide by zero"
2     - misuse of shell builtins, according to Bash documentation
        Seldom seen, usually defaults to exit code 1
126   - Command invoked cannot execute
        Permission problem or command is not an executable
127   - "command not found"   possible problem with $PATH or a typo
128   - Invalid argument to exit (exit 3.14159)
        Exit takes only integer args in the range 0 - 255
128+n - Fatal error signal "n" (kill -9 $PPIDof script)
        $? returns 137 (128 + 9)
130   - Script terminated by Control-C
        Control-C is fatal error signal 2, (130 = 128 + 2, see above)
255   - Exit status out of range (exit -1)
        Exit takes only integer args in the range 0 - 255

Exit codes 1 - 2, 126 - 165, and 255 have special meanings, and should therefore
be avoided as user-specified exit parameters. Ending a script with exit 127
would certainly cause confusion when troubleshooting (is the error a "command
not found" or a user-defined one?). However, many scripts use an exit 1 as a
general bailout upon error. Since exit code 1 signifies so many possible errors,
this might not add any additional ambiguity, but, on the other hand, it probably
would not be very informative either.

There has been an attempt to systematize exit status numbers
(see /usr/include/sysexits.h), but this is intended mostly for C and C++
programmers. It would be well to support a similar standard for scripts. It is
proposed to restricting user-defined exit codes to the range 64 - 113 (in
addition to 0, for success), to conform with the C/C++ standard. This would
still leave 50 valid codes, and make troubleshooting scripts more
straightforward.

Issuing a $? from the command line after a shell script exits gives results
consistent with the table above only from the Bash or sh prompt. Running the
C-shell or tcsh may give different values in some cases.
 
--------------------------------------------------------------------------------

/*
 *	@(#)sysexits.h	8.1 (Berkeley) 6/2/93
 *	
 *	/usr/include/sysexits.h
 */

#ifndef	_SYSEXITS_H
#define	_SYSEXITS_H 1

/*
 *  SYSEXITS.H -- Exit status codes for system programs.
 *
 *	This include file attempts to categorize possible error
 *	exit statuses for system programs, notably delivermail
 *	and the Berkeley network.
 *
 *	Error numbers begin at EX__BASE to reduce the possibility of
 *	clashing with other exit statuses that random programs may
 *	already return.  The meaning of the codes is approximately
 *	as follows:
 *
 *	EX_USAGE -- 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_DATAERR -- The input data was incorrect in some way.
 *		This should only be used for user's data & not
 *		system files.
 *	EX_NOINPUT -- An input file (not a system file) did not
 *		exist or was not readable.  This could also include
 *		errors like "No message" to a mailer (if it cared
 *		to catch it).
 *	EX_NOUSER -- The user specified did not exist.  This might
 *		be used for mail addresses or remote logins.
 *	EX_NOHOST -- The host specified did not exist.  This is used
 *		in mail addresses or network requests.
 *	EX_UNAVAILABLE -- A service is unavailable.  This can occur
 *		if a support program or file does not exist.  This
 *		can also be used as a catchall message when something
 *		you wanted to do doesn't work, but you don't know
 *		why.
 *	EX_SOFTWARE -- An internal software error has been detected.
 *		This should be limited to non-operating system related
 *		errors as possible.
 *	EX_OSERR -- An operating system error has been detected.
 *		This is intended to be used for such things as "cannot
 *		fork", "cannot create pipe", or the like.  It includes
 *		things like getuid returning a user that does not
 *		exist in the passwd file.
 *	EX_OSFILE -- Some system file (e.g., /etc/passwd, /etc/utmp,
 *		etc.) does not exist, cannot be opened, or has some
 *		sort of error (e.g., syntax error).
 *	EX_CANTCREAT -- A (user specified) output file cannot be
 *		created.
 *	EX_IOERR -- An error occurred while doing I/O on some file.
 *	EX_TEMPFAIL -- 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.
 *	EX_PROTOCOL -- the remote system returned something that
 *		was "not possible" during a protocol exchange.
 *	EX_NOPERM -- You did not have sufficient permission to
 *		perform the operation.  This is not intended for
 *		file system problems, which should use NOINPUT or
 *		CANTCREAT, but rather for higher level permissions.
 */

#define EX_OK		0	/* successful termination */

#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 */

#endif /* sysexits.h */