Read/Search file being written to giving error due to timing issues


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read/Search file being written to giving error due to timing issues
# 1  
Old 11-16-2009
Read/Search file being written to giving error due to timing issues

The following is a piece of code to rename LOG_FILE_NEW to LOG_FILE once you get a result (either RUNNING or SHUTDOWN)
Code:
RESULT=""
sleep 30
while [ "${RESULT}" = "" ]; do
sleep 10
RESULT=`sed -n '/RUNNING/'p ${LOG_FILE_NEW}`
if [ "${RESULT}" = "" ]; then
RESULT=`sed -n '/SHUTTING_DOWN/'p ${LOG_FILE_NEW}`
fi
done
mv ${LOG_FILE_NEW} ${LOG_FILE}

However, sometimes I get the following error.
sed: can't read x_new.log: No such file or directory
sed: can't read x_new.log: No such file or directory
sed: can't read x_new.log: No such file or directory


Seems to be a timing issue.

Anyone, any ideas?

Last edited by pludi; 11-16-2009 at 09:09 AM.. Reason: code tags, please...
# 2  
Old 11-16-2009
It is always good practice to test for a file's existence prior to trying to move it.It would help if you stated what shell you are using and what the file variables are set to. If this is ksh then I think you should be using ==, I don't know any other shells. The most likely cause of the problem is the test for "" is failing as you are getting something unexpected back from the sed's. I would use grep -c myself and test for
Code:
-gt 0

It makes life a lot easier if you use code tags when posting this sort of thing.
# 3  
Old 11-16-2009
Bourne shell

And LOG_FILE_NEW is set to /data/x_new.log
LOG_FILE to /data/x.log

This error doesnt happen always. It happens occasionally though either RUNNING or SHUTTING_DOWN do get written to the log.

I also tried to use lsof but it always returns 0. So it doesnt help.

Code:
RESULT=""
WRITE=0
sleep 30
while [ ${WRITE} = "0" ]; do
   sleep 10
   lsof ${LOG_FILE_NEW} > /dev/null 2>&1
   WRITE=$?
done
RESULT=`sed -n '/RUNNING/'p ${LOG_FILE_NEW}`
if [ "${RESULT}" = "" ]; then
   RESULT=`sed -n '/SHUTTING_DOWN/'p ${LOG_FILE_NEW}`
fi

mv ${LOG_FILE_NEW} ${LOG_FILE}

# 4  
Old 11-16-2009
You are testing the sed output to see if it is an empty string but if the log file is not there RESULT =

Code:
sed: can't read x_new.log: No such file or directory



---------- Post updated at 05:33 PM ---------- Previous update was at 03:01 PM ----------

I think at a minimum you want to do something like this: -

Code:
${LOG_FILE_NEW}=x_new.log
${LOG_FILE}=some_log.log

[[ -a ${LOG_FILE_NEW} ]] && echo "Log file ${LOG_FILE_NEW} not found, exiting" && exit 1

while [[ true ]]
do
        [[ $(egrep -c "SHUTTING_DOWN|RUNNING" ${LOG_FILE_NEW}) -ne 0 ]] && break
        sleep 10
done
mv ${LOG_FILE_NEW} ${LOG_FILE}

Now you are not dependent on sed returning an empty string.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

2. UNIX for Advanced & Expert Users

How to read a fast written log file at Real time speed?

Hello All, I am building a real time parser for a log file in my application. The log file is continuously written at a very fast pace and gets rolled over every 10 minutes. I have measured the speed and observed that around 1000 lines are written to it every second, each line about 30-40... (7 Replies)
Discussion started by: cool.aquarian
7 Replies

3. AIX

Opening a file in vi editor is giving out of memory error

Below is the error: Out of memory saving lines for undo - try using ed : Please help me how do I open this file. Best regards, Vishal (5 Replies)
Discussion started by: Vishal_dba
5 Replies

4. UNIX for Advanced & Expert Users

Timing READ command - any key to exit

Hello everyone, I would like some help on an issue I have related to the read command with ksh93 (Unix AIX). I want to implement a 'press any key to exit' scenario. I have the following code: STTY=$(stty -g) if ;then stty -echo -icanon time 0 min 0 fi k="" while ];do read k #... (5 Replies)
Discussion started by: gio001
5 Replies

5. Shell Programming and Scripting

sed -i option giving error no such file or directory

I created a shell with sed -i option. It is giving error - No such file or directory Ex - sed -i 's/yes/no' yes.txt sed -i 's/why/where' yes.txt sed -i 's/when/how' yes.txt Error - :No such file or directory When I run single line in my script say sed -i 's/yes/no' yes.txt... (10 Replies)
Discussion started by: yahoo
10 Replies

6. Shell Programming and Scripting

Issues using array credentials to read contents of a file

Hi, I am trying to read the contents of a file using array credentials in unix. The file I am trying to read is tab separated and contains the below contents. # partnerid Direc Server Port Source_Dir Target_Dir Mask Remove Files Passwordless Compare Files ... (3 Replies)
Discussion started by: aartikara
3 Replies

7. UNIX for Dummies Questions & Answers

No process to read data written to a pipe on AIX

We use SAP application cluster on AIX. Communication between 2 of its instances is failing randomly with the following error: java.net.SocketException: There is no process to read data written to a pipe. The above error causes a cluster restart if an important communication fails. Can... (0 Replies)
Discussion started by: RoshniMehta
0 Replies

8. AIX

Tape drive problem - no process to read data written to a pipe

Hi Everyone, The machine I'm working on is an AIX 5.3 LPAR running on a P650. oslevel -r shows 5300-08. I'm trying to take a backup to a SCSI tape drive, which has been working up until this point. I know of nothing that has changed recently to cause this problem. But when I try to take a... (0 Replies)
Discussion started by: need2bageek
0 Replies

9. UNIX for Advanced & Expert Users

AIX 5.3 - There is no process to read data written to a pipe

I have the following code which works on AIX 4.3 but fails at times on AIX 5.3 with: cat: 0652-054 cannot write to output. There is no process to read data written to a pipe. validator="${validator_exe} ${validator_parms}" cmd_line="${CAT} ${data_file} | ${validator}... (6 Replies)
Discussion started by: vigsgb
6 Replies
Login or Register to Ask a Question