0

I'm trying to make my code play nicely with the other children on Linux systems, and I want to use exit codes that make sense to the shell.

In the specific case I'm starting with, when attempting to write to a directory that does not exist, I find two candidate codes:

#define EX_CANTCREAT 73  /* can't create (user) output file */

in sysexits.h and (effectively):

errno.ENOENT = 2         # No such file or directory

in Python's errno module.

Is one more appropriate than the other for a sys.exit()?

I note that Python provides errno and there is a corresponding errno.h but I'm not seeing anything Pythonic prebuilt for sysexits.h...


EDIT: I was trying to determine if I should be using more specific exit codes and if so, from which set of predefined codes. The answers below give a pretty definite "No": Stick to 0 for success and 1 for failure as exit codes. Use the errno codes for stderr messages, and, for Linux at least, just stay away from sysexits.h... I think.

Ubuntourist
  • 743
  • 10
  • 26
  • Possible duplicate of [Are there any standard exit status codes in Linux?](https://stackoverflow.com/questions/1101957/are-there-any-standard-exit-status-codes-in-linux) – Daniel Pryden Oct 16 '18 at 18:06
  • 1
    Specifically, see this answer: https://stackoverflow.com/a/40484670. – Daniel Pryden Oct 16 '18 at 18:07
  • Not exactly a duplicate question, though the answer @DanielPryden linked to in his suggestion was quite helpful. More like "duplicate answer". ;-) – Ubuntourist Oct 16 '18 at 19:20
  • That's actually the point of duplicate questions on Stack Overflow: "duplicate" doesn't mean "bad question", it means "let's consolidate efforts to answer all these questions in this one place that already has answers". Often the same answers are helpful for lots of different (but related) questions. – Daniel Pryden Oct 16 '18 at 21:15

1 Answers1

1

As a rule (specifically, precedent set by UNIX), a program only uses a few exit codes: 0 for success; optionally, one for each other valid outcome that is not an error (e.g. for grep -- if no matches were found); and one for any error. The code for "any error" is usually the highest. Some programs use special codes for some errors, but these are few and far between.

(A number of exit codes (in UNIX, 128+, in Windows, a few NTSTATUS values that look like large negative numbers) are used if the OS terminates the process -- it's thus not useful to generate them in the program, to avoid confusion.)

Any more specific information about the error is supposed to be printed on stderr.


sysexits.h seems to be specific to BSD and OSX (which is derived from BSD) and is not a part of the POSIX standard.


The motivation for a "catch-all" error exit code is that without additional information, the receiving process cannot do anything intelligent about the error anyway. stderr is a much better way to pass such information.

ivan_pozdeev
  • 28,628
  • 13
  • 85
  • 130