I want to tail a log file which will be written nonstop, the problem is that in my script I don't want to specify a number of lines or bytes etc.. to tail, I want to specify in my script that every time tail the last lines which were not tailed before. how can I do that in my script?? thank you
Asked
Active
Viewed 441 times
2 Answers
0
I think this answer from 2010 is basically the way to go, although you aren't specifying the shell you are using: How to tail -f the latest log file with a given pattern
Essentially the idea is to save the snippet of the last time you tailed the file and use that snippet to output everything that comes after that snippet in your next tail of the file.

Community
- 1
- 1
-
thank you for your answer.I went through your link, but I think my problem is a bit different. I don't have multiple log files, I just have one, which gets bigger by in a pace of 1000 lines per second( the speed varies greatly of course) , I want my script to check the last lines each time , and don't check the same lines twice. I am using bash by the way. – Meysam Rezaei May 20 '15 at 13:25
0
I finally handle it like this:
#!/bin/bash
###Getting the file size (bytes)
end_of_file=`ls -la /root/mbox | cut -d ' ' -f5`
while sleep 1
do
### tail from previous end_of_file to the end
tout_finder=`tail -c+$end_of_file /root/mbox | grep -q "desired words" ; echo $?`
if [ $tout_finder == "0" ]; then
//mycode
###getting new size of file
end_of_file_new=`ls -la /root/mbox | cut -d ' ' -f5`
###replacing it with old one
end_of_file=$end_of_file_new
else
//mycode
###getting new size of file
end_of_file_new=`ls -la /root/mbox | cut -d ' ' -f5`
###replacing it with old one
end_of_file=$end_of_file_new
fi
done

Pang
- 9,564
- 146
- 81
- 122

Meysam Rezaei
- 1
- 4