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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
05-14-2012, 01:13 PM
|
#1
|
Member
Registered: May 2012
Posts: 60
Rep: 
|
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.
|
|
|
05-14-2012, 01:25 PM
|
#2
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
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.
|
05-14-2012, 01:32 PM
|
#3
|
Member
Registered: May 2012
Posts: 60
Original Poster
Rep: 
|
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 
|
|
|
05-14-2012, 04:03 PM
|
#4
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
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.
|
05-14-2012, 06:22 PM
|
#5
|
Member
Registered: Dec 2011
Posts: 114
Rep:
|
why not to mail the required log file through cron job once a day to your boss ?
|
|
1 members found this post helpful.
|
05-15-2012, 09:50 AM
|
#6
|
Member
Registered: May 2012
Posts: 60
Original Poster
Rep: 
|
Still trying to get my head around this. Will update when I do!
|
|
|
05-15-2012, 10:09 AM
|
#7
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
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.
|
05-15-2012, 10:28 AM
|
#8
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
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.
|
05-15-2012, 01:26 PM
|
#9
|
Member
Registered: May 2012
Posts: 60
Original Poster
Rep: 
|
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 
|
|
|
All times are GMT -5. The time now is 05:08 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|