/dev/null 2>&1 question


 
Thread Tools Search this Thread
# 1  
/dev/null 2>&1 question

Hi,

suppose you have the following line at your crontab :

5 * * * * /usr/mine/script > /dev/null 2>&1

now i understood that the " > /dev/null 2>&1 outputs both Standard outpout and Standard Error messages to the /dev/null device or file...

the first part , " > /dev/null " transfers the stdout messages to the /dev/null, but i am missing the part where the stderr is also dumped at dev/null.....

2(is stderr) >(send) &1 (stdout)...

i am not understanding how you send stdout first to dev/null and then send stderr to stdout which in turn will be sent to dev/null too ???

Thanks.
# 2  
There is a system file table. It has one entry for every open file. Your process has descriptors that point to file table entries. Let's change the example to a text file.

So the shell syntax starts out " > results.txt". To run that the shell will need to do something like:
close(1);
fd=open("results.txt", O_WRONLY, 0);
We actually had to open a file. This filled in a file table entry. And now fd 0 points to that file table entry.

Now the syntax continues "2>&1" and to run that, the shell does something like:
close(2);
fd=dup(1);
Here we don't open a file. It's just that whatever file table entry fd 1 was pointing to has been copied into fd 2.

There is more to this than just saving an expensive open call. If we write to fd 1, what ever we write goes into the file. If we then write some more, the new data goes at the end of the file. We can write to fd 1 and we can write to fd 2 and they don't step on each other. That's because a write to either fd updates the file pointer in the file table entry.

In contrast, " > results.txt 2 > results.txt" gives us two file table entries and two file pointers.
 

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Housekeeping null 2>&1 in /dev
only
Hello, Does anyone know how to housekeeping the null 2>&1 file in /dev? its fill up my system, please help. Thanks :b:... AIX
3
AIX
Size of file /dev/./null 2>&1
primo102
Hello Can I just remove/delete flile ? rw-r--r-- 1 root system 2385088512 Jun 30 21:25 /dev/null 2>&1 size of this flile is 2274.75 m and fill up my filesystem: Filesystem MB blocks Used Free %Used Mounted on /dev/hd4 2560.00 2558.59 1.41 100% /... AIX
10
AIX
Redirecting standard out to /dev/null goes to file "/dev/null" instead
jbmorrisonjr
I apologize if this question has been answered else where or is too elementary. I ran across a KSH script (long unimportant story) that does this: if ; then CAS_SRC_LOG="/var/log/cas_src.log 2>&1" else CAS_SRC_LOG="/dev/null 2>&1" fithen does this: /usr/bin/echo "heartbeat:...... Shell Programming and Scripting
5
Shell Programming and Scripting
/dev/null 2>&1 Versus /dev/null 2>1
glev2005
How are these two different? They both prevent output and error from being displayed. I don't see the use of the "&" echo "hello" > /dev/null 2>&1 echo "hello" > /dev/null 2>1... UNIX for Dummies Questions & Answers
3
UNIX for Dummies Questions & Answers
Difference between ">/dev/null 2>&1" and "2>&1 >/dev/null"
proactiveaditya
Does >/dev/null 2>&1 and 2>&1 >/dev/null mean the same?... Shell Programming and Scripting
4
Shell Programming and Scripting
UNIX and Linux

Unix (trademarked as the UNIX certification mark) is a very mature family of multitasking, multiuser computer operating systems that evolved from the original AT&T Unix. Unix development starting in the 1970s by legendary Bell Labs programmers Ken Thompson, Dennis Ritchie, and others. Unix was first targeted for the Bell System and AT&T licensed Unix to outside parties. In the 1970s time period, this lead to a variety of for-profit as well as not-for-profit Unix variants.
In the early days, this included the University of California, Berkeley (BSD), Microsoft (Xenix), IBM (AIX), and Sun Microsystems (Solaris). In the early 1990s, AT&T sold their Unix rights to Novell. In 1995 Novell sold their Unix business to the Santa Cruz Operation (SCO). The UNIX trademark was passed to The Open Group, a "neutral" industry consortium. The Open Group promoted the use of the UNIX trademark for certified operating systems that comply with the Single UNIX Specification (SUS). In 2014 Apple's macOS became the Unix version with the largest global install base and macOS remains the largest Unix-user base today.
Linux is a family of open source Unix-like operating systems based on the Linux kernel. The Linux kernel is an operating system kernel first released by Linus Torvalds on 17 September 1991. Linux distributions include the Linux kernel, system software and libraries. Popular free open source Linux distributions include Debian, Fedora, and Ubuntu. Commercial Linux distributions include Red Hat Enterprise Linux and SUSE Linux Enterprise Server. Linux may be freely modified and redistributed. Anyone on the planet may create a Linux distribution for any purpose.
Please enjoy and help our forum community by "showing your work" and posting your code, even when your code is not working as expected. To help others help you, please wrap your code blocks, sample input, sample output, error messages, and other data in CODE tags and wrap your short commands and short data objects in ICODE tags. We were all beginners in the beginning. If you have any questions about how to register or how to post, please contact us in Live Chat. Thank you and enjoy this "forever free" technical support community for UNIX, Linux and computer information technology in general.