Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a very basic understanding of redirects, but I have come across a cronjob with redirects which I do not understand.

00 19 * * 1-5 /apps/app/scripts/doit.sh a np cron > /apps/app/scripts/doit.log > /dev/null 2>&1

I see three redirects. I was going to write down my best guess here, but I can't form a full picture. I would appreciate it if someone helped me understand in STDOUT and STDERR terms.

My shell is ksh:

:> ps -p $$
  PID TTY          TIME CMD
23947 pts/0    00:00:00 ksh
share|improve this question
1  
What shell are you using? the result is very different between zsh and bash. –  Patrick Dec 26 '13 at 19:45
add comment

1 Answer

up vote 5 down vote accepted

There are 2 redirects there. The last bit, 2>&1 is actually merging STDERR in with STDOUT. This looks to me like someone set this up to log output to the doit.log file but then wanted to disable it.

Chaining redirects in this manner, basically negates the earlier ones, so that only the output, if there is any, will get directed to the last redirected file.

Example

$ echo "string" > 1.txt > 2.txt 2>&1

Resulting in these files:

$ ls -l 1.txt 2.txt
-rw-rw-r--. 1 saml saml 0 Dec 26 15:12 1.txt
-rw-rw-r--. 1 saml saml 7 Dec 26 15:12 2.txt

$ more 1.txt 2.txt 
::::::::::::::
1.txt
::::::::::::::
::::::::::::::
2.txt
::::::::::::::
string

So as you can see file 1.txt is empty, and all the output was directed to the last file, 2.txt.

So why do this?

As I mentioned, my guess would be a syadmin or whomever maintains this, started collecting the output to doit.log initially, but then once things were stabilized, or the doit.log was extra overhead that they no longer needed; they tacked a > /dev/null to quiet the output from the cron.

share|improve this answer
1  
This make sense, since the log file is empty. Does the 2>&1 take effect for all redirects, or just the last (the one that it comes directly after)? –  Nate Dec 26 '13 at 20:29
2  
It merges all the output from STDERR and redirects it to STDOUT. This happens prior to anything being written to a file. –  slm Dec 26 '13 at 20:36
add comment

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.