Break out of tail -f


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Break out of tail -f
# 8  
Old 03-12-2013
I tried this one. Got "[: too many arguments " on the if condition. Any typos in the code below? Thanks.


Quote:
Originally Posted by hanson44
Maybe something like this, if I understand correctly:

Code:
while sleep 10; do
  grep STARTUP nohup.out > temp.x
  lines=`wc -l temp.x`
  if [ $lines -gt 1 ]; then
    echo STARTUP occured twice in nohup.out
    # Do whatever needs doing
  fi
done


Last edited by sai2013; 03-12-2013 at 12:59 PM..
# 9  
Old 03-12-2013
A C/C++/JAVA/PERL/Python level app can keep track of where it has looked and just look at new areas much more easily. Shell has to live with tail -f or some sort of polling the whole file. You might be able to seek in shell using 'tail -####c', where you check between the last and current file size (from ls -l?), then reset the last file size to that current size. If the file size is the same, sleep some more.Rotating logs adds a whole new level of complexity. If you had checked file N up to byte M and it is rotated, you have to detect that and start with Byte M of file N at the new name, then move to the current file. You high water mark is a file number and a byte count.
# 10  
Old 03-12-2013
I do have a single script for both Server Startup & Shutdown with parameters defining the operation & Search Keyword. In both cases, we are monitioring the same file.

There are two scripts - Startup & Shutdown. I added a line at the end of each to execute my script which reads the Server log file.

Once the server starts, we send a message to Syslog and stop reading the log file.

At a later point, when someone decides to stop the server using the shutdown script, it will again invoke my script (which looks for the SHUTDOWN keyword in the log file and send the message to syslog).





Quote:
Originally Posted by RudiC
Wouldn't it make much more sense, esp. as both scripts are managed by you, to write one single script that intercepts both keywords and triggers the respective reaction?
---------- Post updated at 11:58 AM ---------- Previous update was at 11:44 AM ----------

I dont have to be concerned about change in file name. Since, my source file name will not change though we have log files rotating.

Quote:
Originally Posted by DGPickett
If the file size is the same, sleep some more.
File size will continually change. during startup, during the time server is up and when the server is shutdown.

Quote:
Originally Posted by DGPickett
A C/C++/JAVA/PERL/Python level app can keep track of where it has looked and just look at new areas much more easily. Shell has to live with tail -f or some sort of polling the whole file. You might be able to seek in shell using 'tail -####c', where you check between the last and current file size (from ls -l?), then reset the last file size to that current size. If the file size is the same, sleep some more.Rotating logs adds a whole new level of complexity. If you had checked file N up to byte M and it is rotated, you have to detect that and start with Byte M of file N at the new name, then move to the current file. You high water mark is a file number and a byte count.
# 11  
Old 03-12-2013
Who is writing it when the server is shutdown? Smilie Wel, as it shuts down, and then it goes quiescent, I hope!

The idea is that tail -f on a pipe might not process the shutdown due to stuff stuck in the pipe. I think it does, but you seem to have problems, and tail version do vary. The alternative to tail -f if you have tail -####c that seeks, is something like:
Code:
seek=0
while :
 do
  sz=0`ls -l logfile | awk '{print $5}'`
  if (( sz == seek ))
   then
    sleep 1        # or whatever frequency you desire for quiet times
    continue
   fi
  tail -${seek}c $logfile | sed -n '
    /pattern1/w file1
    /pattern2/w file2
    /pattern3/w file3
    /pattern4/w file4
   '
  seek=$sz
 done

Since tail reads to EOF and ends, no data is trapped in the pipe. The sed approach allows you to write marker files for each critical message.
# 12  
Old 03-12-2013
Pickett, I tried the below code. The logs just hung at a point, maybe before reaching the line, '$SERVER_STATUS'


Code:
 
seek=0
while :
do
sz=`ls -l nohup.out | sed '
s/^[^ ][^ ]* *[^ ][^ ]* *[^ ][^ ]* *[^ ][^ ]* *\([^ ][^ ]*\) .*/\1/
'`
if (( sz == seek ))
then
sleep 1
continue
fi
tail -${seek}c nohup.out | sed -n '
/$SERVER_STATUS'/w nohup.out
'
seek=$sz
done

# 13  
Old 03-12-2013
Quote:
Originally Posted by DGPickett
A C/C++/JAVA/PERL/Python level app can keep track of where it has looked and just look at new areas much more easily. Shell has to live with tail -f or some sort of polling the whole file. You might be able to seek in shell using 'tail -####c', where you check between the last and current file size (from ls -l?), then reset the last file size to that current size. If the file size is the same, sleep some more.Rotating logs adds a whole new level of complexity. If you had checked file N up to byte M and it is rotated, you have to detect that and start with Byte M of file N at the new name, then move to the current file. You high water mark is a file number and a byte count.
On the topic of seeking with the shell, the following ksh93 operators may interest you:
Quote:
<# ((expr))
Evaluate arithmetic expression expr and position file descriptor 0 to the resulting value bytes from the start of the file. The variables CUR and EOF evaluate to the current offset and end-of-file offset respectively when evaluating expr.

># ((offset))
The same as <# except applies to file descriptor 1.

<#pattern
Seeks forward to the beginning of the next line containing pattern.

<##pattern
The same as <# except that the portion of the file that is skipped is copied to standard output.
Regards,
Alister
# 14  
Old 03-12-2013
Well, you can *seek() to an offset, or you can search to a pattern, which is *read(). Many systems have ksh93 as dtksh in the CDE bin!

Yes, that loops forever. If you want it to break, make it 'while [ ! -f fileN ]', where fileN is the file created by sed w for the end pattern.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tail +

because the tail +2 on the first line gives me the file name pomga I do not want anything like what I miss tail +2 ejemplo.txt ouput ==> ejemplo.txt <== 1 2 3 4 5 6 7 8 9 10 (2 Replies)
Discussion started by: tricampeon81
2 Replies

2. Shell Programming and Scripting

Joining multiple files tail on tail

I have 250 files that have 16 columns each - all numbered as follows stat.1000, stat.1001, stat.1002, stat.1003....stat.1250. I would like to join all 250 of them together tail by tail as follows. For example stat.1000 a b c d e f stat.1001 g h i j k l So that my output... (2 Replies)
Discussion started by: kayak
2 Replies

3. Shell Programming and Scripting

BASH: Break line, read, break again, read again...

...when the lines use both a colon and commas to separate the parts you want read as information. The first version of this script used cut and other non-Bash-builtins, frequently, which made it nice and zippy with little more than average processor load in GNOME Terminal but, predictably, slow... (2 Replies)
Discussion started by: SilversleevesX
2 Replies

4. Shell Programming and Scripting

break: cannot break

hi guys I am working on a menu for linux... some basic stuff. but I have an issue. I got 1 server where something is working and the same thing does not work in the same way in another linux box Basically I am simulating a command line where user insert some commands and to end and go back... (7 Replies)
Discussion started by: karlochacon
7 Replies

5. Shell Programming and Scripting

help on page break

Hi, i have a file say samp.s which has 123 a b c d 123 e f g h 123 i j k l 123 m n o p 234 a b c d 234 e f g h 234 i j k l the first 3 characters in each line are considered the key values i have one more file temp.txt which has 123 234 i want to have a page break in... (5 Replies)
Discussion started by: Sheema
5 Replies

6. Shell Programming and Scripting

break out of 'if'

is it possible? because i still need to keep on reading even though i don't want to read that particular line (7 Replies)
Discussion started by: finalight
7 Replies

7. HP-UX

Break mirror so that it can be used later

Hello again, We need to install patches to HP-UX B.11.11 but would like to break the mirror it has (with out damaging it) so that in case of failure we can use this a meassure procedure. Any ideas on how to do this Thank you! (2 Replies)
Discussion started by: AQG
2 Replies

8. Shell Programming and Scripting

tail -f

I am trying to extract a particular line from a.log which keeps appending every sec and output that into a newfile b.log which should append itself with filtered data received from a.log I tried tail -f a.log |grep fail| tee -a b.log nothing in b.log tail -f a.log |grep fail >>b.log ... (4 Replies)
Discussion started by: wannalearn
4 Replies

9. Shell Programming and Scripting

Tail??

Hello all, I have search the forum and could not find an answer...Here is what I am trying to do. Every 15 minutes, a script send uptime output to a logfile (dailylog.log), that file contains lines like the one below: 11:21am up 44 days, 19:15, 1 user, load average: 0.00, 0.02, 0.03 ... (7 Replies)
Discussion started by: qfwfq
7 Replies

10. Shell Programming and Scripting

using tail -f

Working in HP-UX 10.20. I eventually want to write a bourne shell script to handle the following problem, but for now I am just toying with it at the command line. Here's what I am basically trying to do: tail -f log_X | grep n > log_Y I am doing a tail -f on log_X . Once it sees "n", I... (6 Replies)
Discussion started by: cdunavent
6 Replies
Login or Register to Ask a Question