/dev/null 2>&1 question


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers /dev/null 2>&1 question
# 1  
Old 04-29-2003
/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  
Old 04-29-2003
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.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Housekeeping null 2>&1 in /dev

Hello, Does anyone know how to housekeeping the null 2>&1 file in /dev? its fill up my system, please help. Thanks :b: (3 Replies)
Discussion started by: only
3 Replies

2. AIX

Size of file /dev/./null 2>&1

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% / (10 Replies)
Discussion started by: primo102
10 Replies

3. Shell Programming and Scripting

Redirecting standard out to /dev/null goes to file "/dev/null" instead

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:... (5 Replies)
Discussion started by: jbmorrisonjr
5 Replies

4. UNIX for Dummies Questions & Answers

/dev/null 2>&1 Versus /dev/null 2>1

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 (3 Replies)
Discussion started by: glev2005
3 Replies

5. Shell Programming and Scripting

Difference between ">/dev/null 2>&1" and "2>&1 >/dev/null"

Does >/dev/null 2>&1 and 2>&1 >/dev/null mean the same? (4 Replies)
Discussion started by: proactiveaditya
4 Replies

6. Programming

Question about NULL Character & fgets()

Assume client send the message " Hello ", i get output such as Sent mesg: hello Bytes Sent to Client: 6 bytes_received = recv(clientSockD, data, MAX_DATA, 0); if(bytes_received) { send(clientSockD, data, bytes_received, 0); data = '\0';... (2 Replies)
Discussion started by: f.ben.isaac
2 Replies

7. Shell Programming and Scripting

Meaning of "> /dev/null 2>&1"

Hi, I am new into UNIX shell scripting and I am wondering what is the meaning of the below text which appears at the end of each line in the ".sh" file: > /dev/null 2>&1 For example, the line below: sh $HOME/stats/Rep777/Act_777.sh omc omc > /dev/null 2>&1 I know for sure what "sh... (10 Replies)
Discussion started by: salanalani
10 Replies

8. Solaris

What is /dev/tty /dev/null and /dev/console

Hi, Anyone can help My solaris 8 system has the following /dev/null , /dev/tty and /dev/console All permission are lrwxrwxrwx Can this be change to a non-world write ?? any impact ?? (12 Replies)
Discussion started by: civic2005
12 Replies

9. Shell Programming and Scripting

> /dev/null

hello all, In many shell scripts i found '> /dev/null' , i am not able to get this, will any one please explain why we are using this. thanks sudha (2 Replies)
Discussion started by: rrs
2 Replies

10. UNIX for Dummies Questions & Answers

>/dev/null

Maybe it's an stupid question but remeber... I'm Junior.. I use command line to run programs, and some of them gives a lot of information when, for example, you open a window or other actions. That's really bad because my terminal gets full of unwanted messages, so I use "bin file & >/dev/null"... (1 Reply)
Discussion started by: piltrafa
1 Replies
Login or Register to Ask a Question