D
D
driverx182019-05-27 23:17:46
linux
driverx18, 2019-05-27 23:17:46

Why /dev/null, if possible without it?

I often saw this in the cron: /dev/null 2>&1, and now I decided to figure it out =)
As I understand it, this is supposedly an output to nowhere. So that my script does not output anything as a result, and just all entries fall into / dev / null (a black hole, roughly speaking nowhere). BUT, I wondered why? Why write this, if, perhaps, you can not write? Why should we directly say DISPLAY TO ANYWHERE, why is it better than not saying directly to him: do not output (do not specify /dev/null 2>&1) ?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
vreitech, 2019-05-28
@driverx18

driverx18 , it is believed that any program can have output. if a specific program does not have it, then this is a property of a specific program, I'm talking about the general case.
by default, if it is not specified where to output the standard message stream, then the output goes to the standard output device - the console.
by default, if it is not specified where to output the standard error stream, then the output goes to the standard output device - the console.
if an application launched via cron outputs to the standard message stream and / or to the standard error stream, then cron intercepts this and, depending on the settings, can hack or, more often, pass it to the logging subsystem, put it in the mailbox.
therefore, unless the output of messages and errors is forced to be redirected to another location (for example, to /dev/null), it will usually end up in the mail.

I
index0h, 2019-05-28
@index0h

Here's an example for you: there is a certain service S listening on port 80, you need to make an http request to it once a second via curl (or wget), as soon as S starts to respond with the 200th response, without errors - execute the C command. Any response from curl/wget - expected, even the impossibility to determine the ip from the host, and it does not need to be output to the console, or logged somehow differently.
This is a real life example, used when starting containers via docker-compose, to perform a run sequence and migrations.

C
CityCat4, 2019-05-28
@CityCat4

The absolute majority of programs in Linux cannot be said - do not display anything. There are separate programs with a "silent" mode, but most of them print their errors to the console, sometimes in great detail. In a very large number of cases, you don’t need this output - and it is sent to / dev / null

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question