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?

share|improve this question
1  
FYI: A shorter way of silencing a process is >&- 2>&-. – Zaz Jul 27 '13 at 20:08
10  
@Josh: why make things even more cryptic than they already are? – endolith Nov 26 '13 at 14:48
2  
@Josh This closes the respective FDs, which could make the programs abort. – glglgl Apr 9 '14 at 7:03
    
@glglgl Ahh, didn't realize there was a difference. Thanks. – Zaz Apr 12 '14 at 10:30
3  
@edelans No. That way redirects stderr to the stdout, but then only the original stdout to /dev/null—stderr will still be output. Try the tool at gist.github.com/zigg/344361751c0110419b0f – zigg May 18 '15 at 15:18
up vote 209 down vote accepted

>> /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.

share|improve this answer
    
What does the & symbol indicate in there 2>&1. – Nobody Jun 28 '17 at 9:47
    
& indicates a file descriptor. There are usually 3 file descriptors - standard input, output, and error. – Testing123 Sep 15 '17 at 17:22

This is the way to execute a program quietly, and hide all its output.

/dev/null is a special filesystem object that throws away everything written into it. Redirecting a stream into it means hiding an output.

The 2>&1 part means "redirect both the output and the error streams". Even if your program writes to stderr, that output will not be shown.

share|improve this answer
25  
Actually, 2>&1 actually redirects stderr to stdout. The difference between this and what you claimed is best illustrated by swapping the order of the redirects, e.g. 2>&1 >/dev/null. – zigg Dec 6 '12 at 14:47

Lets divide the >> /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 observed 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 file descriptor

Whenever you executes a program by default operating system open three files STDIN, STDOUT, and STDERR as we know whenever a file is opened the system returns a small integer called as file descriptor. The file descriptor for these files are 0, 1, 2 respectively.

So 2>&1 simply says redirect STDERR to STDOUT

& 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 want to produce no output even in case of some error in the terminal. To explain lets take 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

Even though I am redirecting output to /dev/null it is getting printed into 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
share|improve this answer

/dev/null - standard file that discards all you write to it, but reports that the write operation succeeded. 1 is stdout and 2 is stderr. 2>&1 redirects stderr to stdout. &1 indicates file descriptor(stdout), otherwise(if you use just 1) you will redirect stderr to file named 1. [any command] >>/dev/null 2>&1 redirects all stderr to stdout, and writes all of that to /dev/null.

share|improve this answer

I use >> /dev/null 2>&1 for silent cronjob, cronjob will do the job but not send report to my email.

As far as I know, don't remove /dev/null, it's useful especially when you running cpanel, can be used for throw away cronjob report.

share|improve this answer

As described by the others, writing to /dev/null eliminates the output of a program. Usually cron sends an email for every output from the process started with a cronjob. So by writing the output to /dev/null you prevent being spammed if you have specified your adress in cron.

share|improve this answer

Edit /etc/conf.apf. Set DEVEL_MODE="0". DEVEL_MODE set to 1 will add a cron job to stop apf after 5 minutes.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.