Bizzare behavior on redirect of stdout


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bizzare behavior on redirect of stdout
# 1  
Old 05-28-2015
Bizzare behavior on redirect of stdout

Oracle Linux 5.6 64-bit (derivative of RHEL)

Dear Ann Landers,

This is about as bizarre as anything I've ever seen.
I have a little test script I've been working with. When I redirect stdout to a file, no file. Make a copy of the script to another name. Execute it and redirect stdout, and I get the file.

Starting with the file listing, the file eds_scp_test is the file in question:
Code:
2015-05-28 10:58:38

oracle:$ ls -ltr eds*
-rw-r----- 1 oracle oinstall  5054 Aug 25  2014 eds_audit_report_generic.sql
-rwxr-x--- 1 oracle oinstall  4603 Aug 26  2014 eds_audit_report
-rw-r--r-- 1 oracle oinstall  3348 Apr  6 12:42 edstest.sql
-rwxr-x--- 1 oracle oinstall  2076 May 21 12:42 eds_ftp_test
-rw-r--r-- 1 oracle oinstall 16286 May 27 10:53 eds_ftp_test.log
-rwxr-x--- 1 oracle oinstall  1296 May 28 09:19 eds_scp_test


2015-05-28 10:58:42

oracle:$ cat eds_scp_test
#!/bin/sh
#===========================================================
# Purpose: test scp step from stage_refresh
#
#===========================================================
logfile=/u01/app/oracle/dba/eds_scp_test.log
rm $logfile
export NLS_DATE_FORMAT='dd-Mon-yyyy hh24:mi:ss'
echo ===================================================== 
echo Begin job at `date`
echo ===================================================== 
#===========================================================
# -- get backup files from prodserver 
#
echo ===================================================== 
echo deleteing old backup at `date` 
echo ===================================================== 
cd /backup/stgdb
rm *.rman
rm *.backup
rm *.alog

echo ===================================================== 
echo begin scp at `date` 
echo ===================================================== 
#scp -v oracle@prodserver:/u01/backup/prddb/* .                  
#scp -v prodserver:/u01/backup/prddb/* .  
#scp -q -v prodserver:/u01/backup/prddb/c4q7mt9e_1_1.alog  .  
# ------------------------------------------------------------------------
echo ===================================================== 
echo End job at `date` 
echo Log is at $logfile 
echo ===================================================== 
exit

Just execute the file, and everything is as expected:
Code:
2015-05-28 10:58:55

oracle:$ ./eds_scp_test
rm: cannot remove `/u01/app/oracle/dba/eds_scp_test.log': No such file or directory
=====================================================
Begin job at Thu May 28 10:59:39 CDT 2015
=====================================================
=====================================================
deleteing old backup at Thu May 28 10:59:39 CDT 2015
=====================================================
rm: cannot remove `*.rman': No such file or directory
rm: cannot remove `*.backup': No such file or directory
rm: cannot remove `*.alog': No such file or directory
=====================================================
begin scp at Thu May 28 10:59:39 CDT 2015
=====================================================
=====================================================
End job at Thu May 28 10:59:39 CDT 2015
Log is at /u01/app/oracle/dba/eds_scp_test.log
=====================================================

Execute it again, but redirect stdout to a log file (errout will still go to display)
Code:
2015-05-28 10:59:39

oracle:$ ./eds_scp_test > eds_scp_test.log
rm: cannot remove `*.rman': No such file or directory
rm: cannot remove `*.backup': No such file or directory
rm: cannot remove `*.alog': No such file or directory

But where's the log file?
Code:
2015-05-28 10:59:54

oracle:$ ls -ltr eds*
-rw-r----- 1 oracle oinstall  5054 Aug 25  2014 eds_audit_report_generic.sql
-rwxr-x--- 1 oracle oinstall  4603 Aug 26  2014 eds_audit_report
-rw-r--r-- 1 oracle oinstall  3348 Apr  6 12:42 edstest.sql
-rwxr-x--- 1 oracle oinstall  2076 May 21 12:42 eds_ftp_test
-rw-r--r-- 1 oracle oinstall 16286 May 27 10:53 eds_ftp_test.log
-rwxr-x--- 1 oracle oinstall  1296 May 28 09:19 eds_scp_test

Now, make an exact copy of the subject script:
Code:
2015-05-28 10:59:58

oracle:$ cp eds_scp_test eds_echo_test


2015-05-28 11:00:15

oracle:$ ls -ltr eds*
-rw-r----- 1 oracle oinstall  5054 Aug 25  2014 eds_audit_report_generic.sql
-rwxr-x--- 1 oracle oinstall  4603 Aug 26  2014 eds_audit_report
-rw-r--r-- 1 oracle oinstall  3348 Apr  6 12:42 edstest.sql
-rwxr-x--- 1 oracle oinstall  2076 May 21 12:42 eds_ftp_test
-rw-r--r-- 1 oracle oinstall 16286 May 27 10:53 eds_ftp_test.log
-rwxr-x--- 1 oracle oinstall  1296 May 28 09:19 eds_scp_test
-rwxr-x--- 1 oracle oinstall  1296 May 28 11:00 eds_echo_test

And test it
Code:
2015-05-28 11:00:19

oracle:$ ./eds_echo_test > eds_echo_test.log
rm: cannot remove `/u01/app/oracle/dba/eds_scp_test.log': No such file or directory
rm: cannot remove `*.rman': No such file or directory
rm: cannot remove `*.backup': No such file or directory
rm: cannot remove `*.alog': No such file or directory

And this time we get the expected log file:
Code:
2015-05-28 11:00:31

oracle:$ ls -ltr eds*
-rw-r----- 1 oracle oinstall  5054 Aug 25  2014 eds_audit_report_generic.sql
-rwxr-x--- 1 oracle oinstall  4603 Aug 26  2014 eds_audit_report
-rw-r--r-- 1 oracle oinstall  3348 Apr  6 12:42 edstest.sql
-rwxr-x--- 1 oracle oinstall  2076 May 21 12:42 eds_ftp_test
-rw-r--r-- 1 oracle oinstall 16286 May 27 10:53 eds_ftp_test.log
-rwxr-x--- 1 oracle oinstall  1296 May 28 09:19 eds_scp_test
-rwxr-x--- 1 oracle oinstall  1296 May 28 11:00 eds_echo_test
-rw-r--r-- 1 oracle oinstall   656 May 28 11:00 eds_echo_test.log

Sign me -- Perplexed.
# 2  
Old 05-28-2015
The first time you run it, you get this error, because eds_scp_test.log does not exist:
Code:
rm: cannot remove `/u01/app/oracle/dba/eds_scp_test.log': No such file or directory

The second time, though, you do not get this error because the script actually succeeds in deleting it -- it's the very first thing it does.

Once you delete it, it naturally no longer exists. (Since it's busy, it will remain on disk -- but not in the folder -- until the script finishes. Once nothing is keeping it open, it will vanish for good.)

Last edited by Corona688; 05-28-2015 at 01:50 PM..
# 3  
Old 05-28-2015
Hmm. Another case of getting so close to the code that one doesn't see it any more. In this case, the deleting of the log file within the script was a vestige of the larger script from which this test piece came. And that script was doing its own logging, rather than redirecting the output as a whole, as in my test. One thing I still don't understand though. Given that I was redirecting stdout of the script as a whole (myscript > myscript.log) even with a command inside the script deleting that same file, would not the next command that generates stdout in the script re-create the specified file?
# 4  
Old 05-28-2015
Calling the write() system call does not create a file. Only open() does that, and the only time your program does that is when you do > filename.

In UNIX, where a file is and what its contents are, are separate questions. What really keeps a file's contents is its inode. If you hardlink a file into two different folders, they will share the same inode.

So when you delete a file which happens to be open, it will delete its directory listing, but the file itself -- its inode -- will still exist until you close it. (or the program holding it quits.) After that, it will be freed completely from disk.

This is a common trap new administrators fall into, incidentally. When they find a huge logfile filling up the disk, they delete it -- which makes things worse because the file remains open, but can't be shrunk from the console anymore. Whatever service is keeping it open would have to be stopped or convinced to close it before the disk space becomes free.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 05-28-2015
Ok. I understand. Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Redirect stdout and stderror in child process

I have a problem when i try to create a log file from a daemon process using shell scripting in ubuntu 12. Ultimatly what i want to achieve is run a java/jar file from a script. After scourging the internet i found several solutions to do this, the one i choose is to create a startup script that... (4 Replies)
Discussion started by: Narev
4 Replies

2. Red Hat

Redirect STDOUT and STDERR of chsh

EDIT: Nevermind, figured it out! Forgot to put backslashes in my perl script to not process literals! Hi everyone. I am trying to have this command pass silently. (no output) chsh -s /bin/sh news Currently it outputs. I've tried.... &> /dev/null 1> /dev/null 2>&1 /dev/null 1>&2... (1 Reply)
Discussion started by: austinharris43
1 Replies

3. Shell Programming and Scripting

help with counting processes, bizzare behavior

I have a ksh script (dtksh Version M-12/28/93d on Solaris 10) that is run daily by cron and sometime hangs forever. I need to detect if there is an old copy hung before I start the new run, and if so send an email and exit the script. Here is part of the code: #!/usr/dt/bin/dtksh... (4 Replies)
Discussion started by: 73rdUserID
4 Replies

4. Shell Programming and Scripting

redirect STDOUT to a file in a subshell

Hi, I would like to avoid re-directing line by line to a file. What is the best way to re-direct STDOUT to a file in a subshell? Thanks in advance. Cheers Vj (1 Reply)
Discussion started by: tnvee
1 Replies

5. UNIX for Dummies Questions & Answers

Redirect stdin stdout to multiple files

Hi, i know how to a) redirect stdout and stderr to one file, b) and write to two files concurrently with same output using tee command Now, i want to do both the above together. I have a script and it should write both stdout and stderr in one file and also write the same content to... (8 Replies)
Discussion started by: ysrini
8 Replies

6. Programming

how to redirect back to stdout

In my program, I am using library provided by other. In the library, the cout/cerr is redirected to a file (the file path is known). After I call some methods in the library, I get one side-effect --> The cout/cerr in my own program is also directed to the file. So how can I to redirect... (5 Replies)
Discussion started by: princelinux
5 Replies

7. Programming

redirect stdout

hello again! i use dup2 to redirect stdout. I run what i want, now i want undo this redirection. how can i do that? thanx in advance (7 Replies)
Discussion started by: nicos
7 Replies

8. Shell Programming and Scripting

How to redirect stderr and stdout to a file

Hi friends I am facing one problem while redirecting the out of the stderr and stdout to a file let example my problem with a simple example I have a file (say test.sh)in which i run 2 command in the background ps -ef & ls & and now i am run this file and redirect the output to a file... (8 Replies)
Discussion started by: sushantnirwan
8 Replies

9. UNIX for Advanced & Expert Users

problem with redirect stdout to file

Hi all hope you can help as I am going MAD!!! :eek: The below is in a shell script but the redirection in the sed line does not work and outputs to the screen and the $fname_2 does note get created ????? Can any one help ?? #!/bin/ksh cd /app/ for fname in `ls -1 X*` do sed 1d $fname... (3 Replies)
Discussion started by: mlucas
3 Replies

10. Shell Programming and Scripting

Redirect stdout and stderr

How can I redirect and append stdout and stderr to a file when using cron? Here is my crontab file: */5 * * * * /dir/php /dir/process_fns.php >>& /dir/dump.txt Cron gives me an 'unexpected character found in line' when trying to add my crontab file. Regards, Zach Curtis POPULUS (8 Replies)
Discussion started by: zcurtis
8 Replies
Login or Register to Ask a Question