How do I get my script to monitor a new file using tail?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do I get my script to monitor a new file using tail?
# 1  
Old 06-04-2009
How do I get my script to monitor a new file using tail?

Hi,

I have a script which basically watches a log file for new lines using tail, then takes action based on what is logged. I wrote a script to do this for me and its working great, my only problem is that once per week, this log file is archived to another directory, and a new log is created. I need to monitor the NEW log file, but even though the old one is moved, my tail which is always running (tail -f | while) still seems to be looking at the OLD file. How can I get my script to work and start reading the NEW log file each week? The only way I can think of to do this is to have another script which runs every week that kills my script and re-starts it...

Here is the specific line in my script where it stops and sits forever when the log file is archived (ASAI.log) and a new one is created:
tail -n1 -f /home/dirTalk/current_dir/oamlog/ASAI.log | while read output_line


Here is the entire script:
Code:
#!/usr/bin/ksh
#############################################################################
# Created by Jeff Civitano on 5/27/2009 for Armonk
#############################################################################
CheckforLock()
{
    # See if an instance of setmwiq_remove is already running by looking for setmwiq_remove_lockfile
    cat /home/dtuser/bin/setmwiq_remove_lockfile > /dev/null 2>&1
    if (test $? -eq 0) then
        #setmwiq_remove already running
        return 1
    fi
    if (test $? -ne 0) then
        #setmwiq_remove not running
        return 0
    fi
}
CreateLock()
{
    # Create setmwiq_remove_lockfile
    touch /home/dtuser/bin/setmwiq_remove_lockfile
    echo "setmwiq_remove created setmwiq_remove_lockfile " >> /home/dirTalk/current_dir/oamlog/setmwiq_remove.log
}
DeleteLock()
{
    # Delete setmwiq_remove_lockfile
    rm /home/dtuser/bin/setmwiq_remove_lockfile
    echo "setmwiq_remove deleted setmwiq_remove_lockfile" >> /home/dirTalk/current_dir/oamlog/setmwiq_remove.log
}
# MAIN
# Is the script already running?
CheckforLock  #Invoke the function
CheckforLockrc=$?
if (test $CheckforLockrc -eq 1) then
    exit 0
fi
# script is not running, create lock
CreateLock
date >> /home/dirTalk/current_dir/oamlog/setmwiq_remove.log
db2 "connect to dtdbv220" >> /home/dirTalk/current_dir/oamlog/setmwiq_remove.log
last_profile=0
switch=3
# Get latest profile number from ASAI.log to Remove from setmwiq
tail -n1 -f /home/dirTalk/current_dir/oamlog/ASAI.log | while read output_line 
do
echo $output_line | grep "confirmed for" > /home/dtuser/bin/profile_file
wc -l /home/dtuser/bin/profile_file | awk '{print $1}' | read count
if ((count>0)) then
    cat /home/dtuser/bin/profile_file | awk '{print $8}' | read profile
    cat /home/dtuser/bin/profile_file | awk '{print $5}' | read OnOrOff
    rc=`echo $profile | grep $last_profile | wc -l | awk '{print $1}'`
    if (test $rc -eq 0) then
      if [[ $OnOrOff = "on" ]]; then
        switch=1
      fi
      if [[ $OnOrOff = "off" ]]; then
        switch=0
      fi
      echo "MWI confirmed for [$profile], remove from setmwiq table:" >> /home/dirTalk/current_dir/oamlog/setmwiq_remove.log
      echo "db2 delete from setmwiq where user_id='$profile' and switch=$switch" >> /home/dirTalk/current_dir/oamlog/setmwiq_remove.log
      db2 "delete from setmwiq where user_id='$profile' and switch=$switch" >> /home/dirTalk/current_dir/oamlog/setmwiq_remove.log
      last_profile=$profile
    fi
fi
done
date >> /home/dirTalk/current_dir/oamlog/setmwiq_remove.log
db2 "quit" >> /home/dirTalk/current_dir/oamlog/setmwiq_remove.log
rm /home/dtuser/bin/profile_file
# script finished, delete lock
DeleteLock


Last edited by vgersh99; 06-04-2009 at 02:49 PM.. Reason: code tags, PLEASE!
# 2  
Old 06-04-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
# 3  
Old 06-04-2009
Disclaimer: I use bash, not ksh

use -F or --retry. So:

tail -n1 -f --retry /home/dirTalk/current_dir/oamlog/ASAI.log | while read output_line

or

tail -n1 -F /home/dirTalk/current_dir/oamlog/ASAI.log | while read output_line

MG
# 4  
Old 06-05-2009
-F or --retry doesn't seem to work on my system (AIX 5.3):
$>tail -n1 -F IMC_Stats.log
Usage: tail [-f] [-c Number|-n Number|-m Number|-b Number|-k Number] [File]
Usage: tail [-r] [-n Number] [File]
Usage: tail [+|-[Number]][l|b|c|k|m][f] [File]

$>tail -n1 --retry IMC_Stats.log
Usage: tail [-f] [-c Number|-n Number|-m Number|-b Number|-k Number] [File]
Usage: tail [-r] [-n Number] [File]
Usage: tail [+|-[Number]][l|b|c|k|m][f] [File]

Any other ideas are appreciated...

Quote:
Originally Posted by mglenney
Disclaimer: I use bash, not ksh

use -F or --retry. So:

tail -n1 -f --retry /home/dirTalk/current_dir/oamlog/ASAI.log | while read output_line

or

tail -n1 -F /home/dirTalk/current_dir/oamlog/ASAI.log | while read output_line

MG
# 5  
Old 06-06-2009
Quote:
Originally Posted by lstorm2003
I wrote a script to do this for me and its working great, my only problem is that once per week, this log file is archived to another directory, and a new log is created. I need to monitor the NEW log file, but even though the old one is moved, my tail which is always running (tail -f | while) still seems to be looking at the OLD file. How can I get my script to work and start reading the NEW log file each week? The only way I can think of to do this is to have another script which runs every week that kills my script and re-starts it...

Here is the specific line in my script where it stops and sits forever when the log file is archived (ASAI.log) and a new one is created:
tail -n1 -f /home/dirTalk/current_dir/oamlog/ASAI.log | while read output_line
If you delete the log file you also break the file handle linked with the tail command. Instead of delete the file empty it after archiving with:

Code:
cp /dev/null /home/dirTalk/current_dir/oamlog/ASAI.log

Regards

Last edited by Franklin52; 06-06-2009 at 11:05 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to tail a file with unknown numbers

Hello, I would like to write script to tail a file for different environment But the number of lines are keep changing How can I write a script For example: env could : A, B or C and log files could be a.log, b.log and c.log with the number of lines can change say sometimes it 100 last... (9 Replies)
Discussion started by: encrypt_decrypt
9 Replies

2. Post Here to Contact Site Administrators and Moderators

Need script to monitor open file in Linux

Kindly advice with shell script to monitor open file in linux, if the open file count is greater then 5000 then send me an email.. command : lsof | wc -l (0 Replies)
Discussion started by: adminhelp
0 Replies

3. Shell Programming and Scripting

Shell script to monitor new file in a directory and mail the file content

Hi I am looking for a help in designing a bash script on linux which can do below:- 1) Look in a specific directory for any new files 2) Mail the content of the new file Appreciate any help Regards Neha (5 Replies)
Discussion started by: neha0785
5 Replies

4. Shell Programming and Scripting

Use “tail -f” to monitor and report, but the top line should be always fixed on the screen.

Title: Use “tail -f” to monitor and report, but the top line should be always fixed on the screen. Hi, dear Unix experts, I am trying to find a Unix command (or scripting) on how to continuously display a text file of its last several lines of contents. But during this displaying, I want some... (2 Replies)
Discussion started by: df3c
2 Replies

5. Shell Programming and Scripting

Script to monitor log file

Hi, Have written a script to monitor linux non standard log file based on line numbers, so each check store $otalinenum .. then in next check after 10 minutes it compre the current_total_line_num > last_total_line_num then it will parse the log file from last_total_line_num to... (0 Replies)
Discussion started by: Shirishlnx
0 Replies

6. Shell Programming and Scripting

script to tail file; problem with awk and special characters

Trying to use code that I found to send only new lines out of a log file by doing: while :; do temp=$(tail -1 logfile.out) awk "/$last/{p=1}p" logfile.out #pipe this to log analyzer program last="$temp" sleep 10 done Script works fine when logfile is basic text, but when it contains... (2 Replies)
Discussion started by: moo72moo
2 Replies

7. Shell Programming and Scripting

Script to monitor the pattern in the log file

hi All, how to find a pattern in the log file & display the above and below line for example in the log file, i have many lines, whenever i search for "Category" it should display the above line with only few parameter like i want only the location name & department name Thu Jul 02 11:05:23... (2 Replies)
Discussion started by: rithick256
2 Replies

8. AIX

Aix script to monitor when a file has been updated

Hi Im looking to create a Aix script to monitor a file for once it's been updated and then send an email. I dont have great scripting knowledge and have only come up with the following so far while true do find /home/test/AMQ* -mmin 1 > /home/test/scripts/output/MQERRORS exec... (2 Replies)
Discussion started by: elmesy
2 Replies

9. Shell Programming and Scripting

Bash tail monitor log file

Hi there, I have a problem here that involves bash script since I was noob in that field. Recently, I have to monitor data involve in logs so I just run command tail -f for the monitoring. The logs was generate every hour so I need to quickly change my logs every time the new hour hits according... (2 Replies)
Discussion started by: kriezo
2 Replies
Login or Register to Ask a Question