The C standard says that the stdin , stdout and stderr file streams must be connected somewhere, but they certainly do not indicate where. (C11 §7.21.3. Files No. 7 :
When the program starts, three text streams are predefined and do not need to be opened explicitly - standard input (for reading ordinary input), standard output (for writing regular output) and standard error (for recording diagnostic output). Upon initial opening, the standard error stream is not fully buffered; standard input and standard output streams are fully buffered if and only if it can be determined that the stream does not refer to an interactive device.
It is possible to run a program with redirected standard threads:
some_program_of_yours >/dev/null 2>&1 </dev/null
Your entries will be successful, but the information will not go anywhere. A more brutal way to run your program:
some_program_of_yours >&- 2>&- </dev/null
This time it was launched without open file streams for stdout and stderr - in violation of the standard. In this example, it is still reading from /dev/null , which means that it is not getting any useful data from stdin .
Many programs do not bother to check that standard I / O channels are open. Many programs did not bother to verify that the error message was successfully written. It’s not always worth the effort to come up with a suitable fallback in the form of Tim Post and whitey04 drafts If you run the ls with suppressed outputs, it will just do what it can and exit with a non-zero status:
$ ls; echo $? gls 0 $ ls >&- 2>&-; echo $? 2 $
(Tested by RHEL Linux.) No need to do more. On the other hand, if your program should run in the background and write to the log file, it probably won’t write much to stderr if it cannot open the log file (or does not find an error in the log). file).
Please note that if you return to syslog(3) (or POSIX ), you have no way of knowing if your calls were “successful” or not; all syslog functions do not return status information. You just have to assume that they were successful. Therefore, this is your last resort.
Jonathan leffler
source share