Redirecting stdout problem | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Redirecting stdout problem

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-20-2012
igurov igurov is offline
Registered User
 
Join Date: Jun 2012
Last Activity: 27 June 2012, 3:54 AM EDT
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Redirecting stdout problem

I have a simple bash script that prints sth every 5 seconds. What I do is the following. I redirect the output of the script to a file, tail the file and see that it works and then from another console I delete the file where the output is redirected to. Even though I have deleted the file, the tail still works. Stoping the tail and starting it again of course fails because the file is not found.
My question is whether this is normal and does the output go "somewhere" after deleting the file where it is supposed to be redirected.

Best,
Iliya
Sponsored Links
    #2  
Old 06-20-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 21 October 2014, 6:30 PM EDT
Location: Saskatchewan
Posts: 19,635
Thanks: 816
Thanked 3,335 Times in 3,124 Posts
The file still exists until everything with it open, closes it or quits.

Try truncating it, i.e. overwriting it with nothing, instead of deleting it. Having it open can't prevent that from happening, and tail -f is usually smart enough to seek back to the beginning.


Code:
: > filename

Sponsored Links
    #3  
Old 06-20-2012
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 22 October 2014, 3:21 AM EDT
Location: Варна, България / Milano, Italia
Posts: 5,675
Thanks: 184
Thanked 620 Times in 578 Posts
Yes,
the open file descriptors that refer to the deleted file remain (there will be an unnamed file).

Consider the following example (this is on Linux):


Code:
zsh-4.3.17[t]% zsh -c 'while :; do sleep 3;date ; done > outfile'&
[1] 1470
zsh-4.3.17[t]% ls -l /proc/1470/fd
total 0
lrwx------ 1 drado drado 64 Jun 20 23:08 0 -> /dev/pts/2
l-wx------ 1 drado drado 64 Jun 20 23:08 1 -> /home/drado/t/outfile
lrwx------ 1 drado drado 64 Jun 20 23:08 10 -> /dev/pts/2
lr-x------ 1 drado drado 64 Jun 20 23:08 11 -> /dev/null
lrwx------ 1 drado drado 64 Jun 20 23:08 12 -> /dev/pts/2
lrwx------ 1 drado drado 64 Jun 20 23:08 2 -> /dev/pts/2
zsh-4.3.17[t]% tail -f /home/drado/t/outfile
Wed Jun 20 23:08:08 CEST 2012
Wed Jun 20 23:08:11 CEST 2012
Wed Jun 20 23:08:14 CEST 2012
Wed Jun 20 23:08:17 CEST 2012
Wed Jun 20 23:08:20 CEST 2012
Wed Jun 20 23:08:23 CEST 2012
^C
zsh-4.3.17[t]% rm /home/drado/t/outfile
zsh-4.3.17[t]% tail -f /home/drado/t/outfile
tail: cannot open `/home/drado/t/outfile' for reading: No such file or directory
zsh-4.3.17[t]% tail -f /proc/1470/fd/1
Wed Jun 20 23:08:14 CEST 2012
Wed Jun 20 23:08:17 CEST 2012
Wed Jun 20 23:08:20 CEST 2012
Wed Jun 20 23:08:23 CEST 2012
Wed Jun 20 23:08:26 CEST 2012
Wed Jun 20 23:08:29 CEST 2012
Wed Jun 20 23:08:32 CEST 2012
Wed Jun 20 23:08:35 CEST 2012
Wed Jun 20 23:08:39 CEST 2012
Wed Jun 20 23:08:42 CEST 2012
Wed Jun 20 23:08:45 CEST 2012
^C
zsh-4.3.17[t]% lsof -p1470|fgrep outf
zsh     1470 drado    1w   REG  252,0      630  261137 /home/drado/t/outfile (deleted)
zsh-4.3.17[t]% kill -9 %1
zsh-4.3.17[t]%
[1]  + killed     zsh -c 'while :; do sleep 3;date ; done > outfile'

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
redirecting to stdout in betwen command phoenix_nebula Shell Programming and Scripting 4 03-16-2010 01:09 AM
Redirecting several outputs to /dev/stdout aplaydoc UNIX for Dummies Questions & Answers 1 03-06-2009 10:42 PM
Redirecting part of output to stdout Legend986 Shell Programming and Scripting 2 10-17-2008 03:20 PM
implicitly redirecting stdout to a file ALTRUNVRSOFLN Shell Programming and Scripting 1 09-23-2008 03:42 PM
redirecting STDOUT & STDERR jshinaman Shell Programming and Scripting 9 06-22-2007 12:04 AM



All times are GMT -4. The time now is 04:00 AM.