4

I'm working on refactoring some Python scripts intended to be run from the command line. The scripts use logging, which makes some messages be written to STDERR (among other things). I wonder if it's appropriate for a program in Linux to output to STDERR, but still return a zero (success) exit status. I thought that STDERR should only be consulted if the exit status is non-zero.

P.S. Basically I need to capture the output from the scripts, and send it to another service for processing, along with the exit status. I want the data to be consistent.

6
  • 2
    Not sure about the answer (there is currently a close vote as opinion-based and I agree that, to some extent, "it depends"), but here are a couple of thoughts: the read builtin (at least in Bash, dash, ksh) writes its optional message to the user to standard error, a not-uncommon way to talk to the user in interactive scripts, and nevertheless returns 0 on success; ssh -v writes debugging information on standard error and still returns 0 on success. Commented Jun 23, 2020 at 20:53
  • 4
    POSIX dd is obliged to use stderr on completion, regardless of its exit status. Commented Jun 23, 2020 at 20:56
  • 1
    Your question title asks if a program can do that, and the question itself asks if it's appropriate. Those aren't exactly the same, and the first is completely an objective technical question (yeah, sure it's possible) while the second seems a bit more preference-based (who gets to call what's appropriate?). Commented Jun 23, 2020 at 21:25
  • …  And I don’t understand what your second paragraph (“P.S.”) has to do with either the first paragraph or the title.  Are you asking about a generic program (which we might call a ‘‘child process’’) or a wrapper / controller / supervisor program that runs subordinate programs (so it is a parent process)?  If it’s the latter, then, again, the answer is “it depends”.  Are you concerned only about success or failure of the child, from a bird’s eye view — i.e., is it safe to proceed with subsequent commands?  — or are you concerned about anomalies, which you might want to pass along to a human? Commented Apr 19, 2022 at 17:21
  • @G-ManSays'ReinstateMonica' I was talking about interpreting the results (output to stderr and exit status) together. AFAIR, it was a parent Python process calling subprograms. Commented Apr 20, 2022 at 7:42

3 Answers 3

5

Yes, programs can output to standard error and still return a zero exit status. You will find many POSIX utilities specifications which state that "The standard error shall be used only for diagnostic messages."

A clear example of a program that outputs to STDERR and returns 0 is crontab. Try

EDITOR=nano crontab -e 2> stderr.out; echo $?

Do not modify the cron file and exit the editor. 0 will be echoed and yet there will be some message in stderr.out, such as (in my case)

no crontab for user - using an empty one
No modification made
5

stderr is just a predefined stream with file number 2.  The convention is to print there important or alternative information (it does not have to be an error).

For example, curl will by default output the messages triggered by --verbose to stderr because stdout is already being used to get the actual result. So you will have a zero exit code and output to stderr:

$ curl ifconfig.me

$ curl --verbose ifconfig.me 2>messages.txt

The exit code, on the other hand, is the parameter of sys.exit() (see _exit(2) and exit(3)).  There is no rule linking the two.  Take grep, for example; that will output to stderr in case of error but may adjust the exit code depending on the flags.

Normally the exit status is 0 if a line is selected, 1 if no lines were selected, and 2 if an error occurred.  However, if the -q or --quiet or --silent is used and a line is selected, the exit status is 0 even if an error occurred.

2
  • @ilkkachu the user specifically said "I'm working on refactoring some Python scripts". That's the reason I added the "pythonism" to answer. I understand we try to keep the answers general, but in this case it might make sense to explain it from the perspective of the language being used. Commented Jun 23, 2020 at 21:32
  • ah heck, I'm not sure how I missed that mention reading the question. I blame it on the late hour, sorry. :( Commented Jun 23, 2020 at 21:34
2

Well, I’m still not clear what this question is asking.  But, since it has been reopened, I’ll post what I said earlier.  At the risk of repeating what others (and I) have said:

  1. It is certainly possible for a program to write to stderr and still exit with a status of 0.  While it is unusual, it is not considered bad practice.
  2. If your primary concern is whether it is appropriate / safe to run program N+1 after program N (and possibly other predecessor tasks) have exited, you should probably make that decision based on exit status alone.
  3. If you want to escalate anomalies to the attention of a human, and they aren’t already being logged adequately, then you should look at stderr.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.