LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-14-2012, 01:13 PM   #1
secondhandman
Member
 
Registered: May 2012
Posts: 60

Rep: Reputation: Disabled
Shell script tail -F


Hello to the kind people of linuxquestions.org!

I'm trying to create a script that will gather data from a log file that is constantly updating. I only need to include the newest data every time the script runs. This data will need to be emailed to my boss. Right now he receives an email every 5-10 minutes with the data he needs but would prefer to have a single email a day (or a least fewer).

I found this online but don't know what to make of it:


Tail

With no options it shows the last ten lines of a file.

Use tail -n x (where "x" is a number) to display the last x lines.

Try tail -F to use a continually updated version of tail (if the file changes it will be reloaded and displayed), please note that using this option will run tail is a continuous loop so you'll need to use CTRL -C to exit.

For example:
tail -n 20 somelog.txt


The tail -F option seems like it would work, but I'm not sure what to do. Any help would be appropriated.
 
Old 05-14-2012, 01:25 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,552

Rep: Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112
One option is to have the script run constantly in an infinite loop with a delay and use "cat file | wc -l" to count the number of lines. Each time it runs, it compares the number of lines in the file to the previous number of lines, then passes the result to tail to only send the updates, IE:

Code:
nlines_old=0
while [[ 1 ]]; do
   nlines_new=$(cat file | wc -l)
   if [[ $nlines_new -gt $nlines_old ]]; then
      nlines=$(expr $nlines_new - $nlines_old)
      updates=$(tail -n $nlines)
   
      # Mail $updates
   
      nlines_old=$nlines_new
   fi
   sleep 10
done
It's not perfect though because any delay between the cat | wc -l and the tail will cause it to skip lines. I guess it just depends on how quickly this file is being updated.

Last edited by suicidaleggroll; 05-14-2012 at 06:31 PM.
 
1 members found this post helpful.
Old 05-14-2012, 01:32 PM   #3
secondhandman
Member
 
Registered: May 2012
Posts: 60

Original Poster
Rep: Reputation: Disabled
Thanks for the help!

I'm very new to scripting and am not sure what the commands in the example you gave are doing. Some solid advice though, I'll start looking into that now!

Any links to websites that can explain it to me like I'm an idiot would appreciated
 
Old 05-14-2012, 04:03 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,552

Rep: Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112
It's a very simple script, there are probably better ways to do it

Essentially it runs forever (hence the "while [[ 1 ]]"), with a delay between loops of 10 seconds (the "sleep 10"). Each time it runs through the loop, it checks the number of lines in the file ("cat file | wc -l"). If there are more lines now than the last time the loop ran 10 seconds ago, it enters the if statement. At that point is subtracts the previous number of lines from the current number of lines and stores the result (the number of lines that have been added) in nlines. Then it does a "tail -n $nlines" to grab those new lines and stores them in the variable "updates". It's then up to you to do whatever it is you want to do with that data.
 
1 members found this post helpful.
Old 05-14-2012, 06:22 PM   #5
ratotopi
Member
 
Registered: Dec 2011
Posts: 114

Rep: Reputation: 6
why not to mail the required log file through cron job once a day to your boss ?
 
1 members found this post helpful.
Old 05-15-2012, 09:50 AM   #6
secondhandman
Member
 
Registered: May 2012
Posts: 60

Original Poster
Rep: Reputation: Disabled
Still trying to get my head around this. Will update when I do!
 
Old 05-15-2012, 10:09 AM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,552

Rep: Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112Reputation: 2112
Another option is to copy the log file to a backup at the end of the script, and email the results of a diff at the beginning of the script. IE:

Code:
cp file tempfile
updates=$(diff tempfile difffile)

# Mail $updates

mv tempfile difffile
 
1 members found this post helpful.
Old 05-15-2012, 10:28 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
logtail "reads a specified file (usually a log file) and writes to the standard output that part of it which has not been read by previous runs of logtail".
 
1 members found this post helpful.
Old 05-15-2012, 01:26 PM   #9
secondhandman
Member
 
Registered: May 2012
Posts: 60

Original Poster
Rep: Reputation: Disabled
Thanks for all the reply's. While looking into logtail, I came across a program called logwatch that seems to allow the data to be emailed. It seems like a simpler setup than having to write the script myself, but I'll figure it out! I'll let you know how it goes!

Total noob here by the way
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Linux Shell script - cat or tail syntax charu Programming 7 03-31-2011 07:59 AM
[SOLVED] place a shell script with tail -f in the back ground procfs Linux - Newbie 6 10-06-2010 11:29 PM
Shell script-How to call tail then exit when 'END timestamp' appears in log? Mountain Linux - Software 4 05-01-2009 03:29 PM
Bash: Script with tail and grep Primsi Linux - General 2 11-16-2006 06:30 AM
running root-tail from shell script not working Nuk Linux - Newbie 1 04-09-2004 09:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration