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
-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.