The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Piping tail to awk to parse a log file DeCoTwc Shell Programming and Scripting 4 06-01-2009 06:47 AM
Aix script to monitor when a file has been updated elmesy AIX 2 03-19-2009 04:28 PM
how to tail a log file.. suri.tyson Shell Programming and Scripting 4 10-01-2008 05:21 AM
Grep or Tail in shell script Moxy Shell Programming and Scripting 3 08-03-2008 10:36 PM
Bash tail monitor log file kriezo Shell Programming and Scripting 2 03-28-2006 01:01 AM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 06-04-2009
lstorm2003 lstorm2003 is offline
Registered User
  
 

Join Date: Jun 2009
Location: Fort Lauderdale, FL
Posts: 5
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!
 

Bookmarks

Tags
script problem question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:27 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0