I found this piece of code in /etc/cron.daily/apf
#!/bin/bash /etc/apf/apf -f >> /dev/null 2>&1 /etc/apf/apf -s >> /dev/null 2>&1
It's flushing and reloading the firewall.
I don't understand the >> /dev/null 2>&1
part.
What is the purpose of having this in the cron? It's overriding my firewall rules. Can I safely remove this cron job?
/dev/null is a pseudo-device file in Linux, which is used to discard output coming from programs, especially the ones executed on the command line.
command >> /dev/null 2>&1 1 is standard output and 2 is standard error. 2>&1 redirects standard error to standard output. &1 indicates file descriptor (standard output), otherwise (if you use just 1 ) you will redirect standard error to a file named 1 .
/dev/null in Linux is a null device file. This will discard anything written to it, and will return EOF on reading. This is a command-line hack that acts as a vacuum, that sucks anything thrown to it.
1 "Standard output" output file descriptor. The expression 2>&1 copies file descriptor 1 to location 2 , so any output written to 2 ("standard error") in the execution environment goes to the same file originally described by 1 ("standard output").
>> /dev/null
redirects standard output (stdout
) to /dev/null
, which discards it.
(The >>
seems sort of superfluous, since >>
means append while >
means truncate and write, and either appending to or writing to /dev/null
has the same net effect. I usually just use >
for that reason.)
2>&1
redirects standard error (2
) to standard output (1
), which then discards it as well since standard output has already been redirected.
Let's break >> /dev/null 2>&1
statement into parts:
Part 1: >>
output redirection
This is used to redirect the program output and append the output at the end of the file. More...
Part 2: /dev/null
special file
This is a Pseudo-devices special file.
Command ls -l /dev/null
will give you details of this file:
crw-rw-rw-. 1 root root 1, 3 Mar 20 18:37 /dev/null
Did you observe crw
? Which means it is a pseudo-device file which is of character-special-file type that provides serial access.
/dev/null
accepts and discards all input; produces no output (always returns an end-of-file indication on a read). Reference: Wikipedia
Part 3: 2>&1
(Merges output from stream 2 with stream 1)
Whenever you execute a program, the operating system always opens three files, standard input, standard output, and standard error as we know whenever a file is opened, the operating system (from kernel) returns a non-negative integer called a file descriptor. The file descriptor for these files are 0, 1, and 2, respectively.
So 2>&1
simply says redirect standard error to standard output.
&
means whatever follows is a file descriptor, not a filename.
In short, by using this command you are telling your program not to shout while executing.
What is the importance of using 2>&1
?
If you don't want to produce any output, even in case of some error produced in the terminal. To explain more clearly, let's consider the following example:
$ ls -l > /dev/null
For the above command, no output was printed in the terminal, but what if this command produces an error:
$ ls -l file_doesnot_exists > /dev/null ls: cannot access file_doesnot_exists: No such file or directory
Despite I'm redirecting output to /dev/null
, it is printed in the terminal. It is because we are not redirecting error output to /dev/null
, so in order to redirect error output as well, it is required to add 2>&1
:
$ ls -l file_doesnot_exists > /dev/null 2>&1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With