How to tail real time file Generated every hour?

Hi Guys,

I am developing a script to monitor log file Generated every hour.
When creating a new file scripts not working.

My Code . I want to monitor GatewayTransaction.yyyymmdd-hh.log

while true; do newdate=$(date '+%Y%m%d') ; nowdate=$(date '+%Y%m%d-%H.log'); tail -f /usr/home/ABCD/atimo/log/$newdate/option/OP2/GatewayTransaction.$nowdate ;sleep 5 ; done ;

Ex Log name

-rw-r--r--   1 bewio   adlioj   85730700 Apr 29 08:59 GatewayTransaction.20120429-08.log
-rw-r--r--   1 bewio   adlioj   13162747 Apr 29 09:59 GatewayDR.20120429-09.log
-rw-r--r--   1 bewio   adlioj   45739723 Apr 29 09:59 GatewayTransaction.20120429-09.log
-rw-r--r--   1 bewio   adlioj     25650 Apr 29 10:31 GatewayException.20120429.log
-rw-r--r--   1 bewio   adlioj      1741 Apr 29 10:31 GatewayNegativeResponse.20120429.log
-rw-r--r--   1 bewio   adlioj    195235 Apr 29 10:36 GatewayCIMDDebug.20120429-10.log
-rw-r--r--   1 bewio   adlioj   11630362 Apr 29 10:36 GatewaySMPPDebug.20120429-10.log
-rw-r--r--   1 bewio   adlioj   17897250 Apr 29 10:36 GatewayDR.20120429-10.log
-rw-r--r--   1 bewio   adlioj   73384961 Apr 29 10:36 GatewayTransaction.20120429-10.log

Please Help me or guide line.

Try this:

while true; do newdate=`date '+%Y%m%d'`; nowdate=`date '+%Y%m%d-%H.log'`; tail -f /usr/home/ABCD/atimo/log/$newdate/option/OP2/GatewayTransaction.$nowdate; sleep 5; done;

It Not Work.

What does not work?

Original post compared with suggested change from @spacebar:

The O/P stated that the original problem was "When creating a new file scripts not working".
Both of the above command sequences have this problem (the suggested change just makes an irrelevant change to the date command). They will both go wrong when the application starts a new file because the "tail -f" will lose context.

The big problem is because the script is using "tail -f".
There is no "quick and easy" way round this script design issue except to avoid "tail -f".

My usual approach to log file monitoring is to issue "wc -l" to find out the number of lines in the logfile, record that number in a workfile, then after the time interval calculate the new number of lines, calculate the difference and then issue "tail -n". This is a simplified view because we need to take account of the application starting a new file and will utililise the timestamp on our record file to detect the new file. The simplest approach to the "first time" condition is to do nothing except record the current state. This means that the logfile monitoring script needs to start running exactly one time increment earlier than the first report.

Ps. It always helps to know what Operating System and version you are running and what Shell you prefer.

I don't know which shell you want to use; But if using 'ksh' and you always want to see the end of the file that was last modified you should be able to use something like this:

#!/bin/ksh
while true
  do
    newdate=`date '+%Y%m%d'`
    for x in `ls -tr /usr/home/ABCD/atimo/log/${newdate}/option/OP2/GatewayTransaction.*`
    do
      file=`echo ${x}`
    done
    tail -f $file
  sleep 5
  done

hth

Some tails have a "-F" (retry) option:

     -F      The -F option implies the -f option, but tail will also check to see if the file
             being followed has been renamed or rotated.  The file is closed and reopened when
             tail detects that the filename being read from has a new inode number. 

@Scrutinizer: -F option would work only if the file name doesn't change.

@ooilinlove: One alternative is, you can have a soft link pointing to the log file and the link should be re-created every hour. This can be taken care by an entry in cron.

For e.g., create a soft link 'GatewayTransaction.log' pointing to 'GatewayTransaction.20120429-08.log' in the 8th hour. In the 9th hour it should point to the 9th hour log file. This hourly rolling can be taken care by an entry in cron.

So once this is done, you can use the -F option of tail tail -F GatewayTransaction.log and this would continuously tail log files regardless of the hour.