Monitor and capture the latest entry from the log file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Monitor and capture the latest entry from the log file
# 1  
Old 01-31-2018
Monitor and capture the latest entry from the log file

Hi,

I want to monitor a log file using tail -f command and search for a specific string on the most recent entry from the file. If the search string matches with the most recent or last line from the file, I want send an email to the people with the message.

Code:
tail -f service.log|tail -n 1

But the above code does not seem to work.

Thanks,
S
# 2  
Old 01-31-2018
Your tail | tail pipeline will never end. The 2nd tail reads one line from the end of the pipeline; but, since the tail -f will never terminate, the pipeline won't deliver an EOF to the 2nd tail until you manually kill the 1st tail.

If you want to read the last line of a file, just use tail -n 1 service.log. If you want to search for the first occurrence of a line containing a fixed string on the last line of a file, and, if the string is not found on that line, continue reading lines newly written to that file until that string is found, try something like:
Code:
tail -f -n 1 service.log | grep  -F "fixed string" | while IFS= read -r line
do	echo "found: $line"	# Do whatever you want to do with that line...
	break			# Terminate the pipeline and continue processing
				# with whatever follows the end of this loop.
done

You haven't told us what operating system or shell you're using (which you should always do on the first post in a new thread).

With many shells, the value assigned to the line variable will disappear when you exit the loop. With a few shells, the value assigned to line will remain in force after the loop ends.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 01-31-2018
Thank you. I am working on bourne shell.

Sorry I am a rookie here. As soon as the value (from the last line of the file) matches the search string, I want to send an email and continue to monitor the log for the next message.

Example:

Search String = AWX_38900
file name=service.log

I want to search for the string 'AWX_38900' in the last line of the file service.log and if it finds a match, it has to send an email to the group of people and continue monitoring the service.log for the next message.
# 4  
Old 01-31-2018
Quote:
Originally Posted by svajhala
Thank you. I am working on bourne shell.

Sorry I am a rookie here. As soon as the value (from the last line of the file) matches the search string, I want to send an email and continue to monitor the log for the next message.

Example:

Search String = AWX_38900
file name=service.log

I want to search for the string 'AWX_38900' in the last line of the file service.log and if it finds a match, it has to send an email to the group of people and continue monitoring the service.log for the next message.
Someone with over 100 posts who has been in the forums for more than seven years doesn't really count as a rookie...

Great. So, take the code I suggested for doing what you're trying to do, replace the echo command with a command to invoke your mail sending utility (with an appropriate subject line, destination address, and a message body including the line you captured.

You still haven't told us what operating system you're using so there's no way we can guess at what mail sending utilities might be available to you. But with over 100 posts to your credit, you should be able to come close to solving any remaining issues on your own. See what you can do and show us where you get stuck if you can't get it to work.
# 5  
Old 01-31-2018
Thanks for being judgmental about my seniority on this forum just by looking at the years and number of posts I have posted on this forum. Unix programming is not my mainstream work and I get to work on scripting only once in a while. My most recent post was back in july '17 which clearly indicates that I am not a regular visitor or poster on this forum.
# 6  
Old 02-01-2018
The dictionary on my desk says that a newbie is an inexperienced newcomer to a particular activity. You may be inexperienced, but with the length of time you have been a member of this forum, you are not a newcomer.

I'm sorry you think I'm being judgmental. But, our goal in this forum is to help you learn how to use the utilities available on your operating system to do whatever you're trying to do. We are not here to act as your unpaid programming staff.

If you're unwilling to tell us what operating system you're using and you're unwilling to even attempt to use the unspecified mail sending utility of your choice to send a message to one or more e-mail addresses you have not specified with a subject line you have not specified with a message body you have not specified other than that it is to contain the text in the line variable included in the code template I provided you, there isn't much more that I can do for you. I wish you luck in your endeavor and I'm truly sorry that the suggestions and examples I have provided are of no use to you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Selecting latest entry in the log file

Hi there I am trying to write a script where I will need to look for a specific word in the log file and I am aware this can be done by grep for example. As there will be multiple entries for this I want to grep the last one to enter the log... how would I go about this - would I have to use... (5 Replies)
Discussion started by: simpsa27
5 Replies

2. Shell Programming and Scripting

Need to grep certain entry out of log file

This one is a bit too challenging for me... Hopefully you guys can help. Let's say I have a log file called: "$MW_HOME/user_projects/domains/IDMDomain/servers/wls_ods?/logs/wls_ods1-diagnostic.log" In this log file I want to search for "DIP-10219". When I execute this $ cat... (7 Replies)
Discussion started by: exm
7 Replies

3. HP-UX

Script to monitor /var/opt/resmon/log/event.log file

AM in need of some plugin/script that can monitor HP-UX file "/var/opt/resmon/log/event.log" . Have written a scrip in sh shell that is working fine for syslog.log and mail.log as having standard format, have interrogated that to Nagios and is working as I required . But same script failed to... (3 Replies)
Discussion started by: Shirishlnx
3 Replies

4. Solaris

Crontab latest entry disappearing. plz help

The latest crontab entry is disappearing time and again on acceptance and production environment. the same entry gets deleted. any pointers to what might be causing this issue? (6 Replies)
Discussion started by: bluenavi
6 Replies

5. Solaris

Crontab latest entry disappearing

The latest crontab entry is disappearing time and again on acceptance and production environment. the same entry gets deleted. any pointers to what might be causing this issue? (1 Reply)
Discussion started by: bluenavi
1 Replies

6. Shell Programming and Scripting

Perl's buffered I/O is causing me to miss latest log file entries in log colorizer. How to fix?

I've been finding myself using a log file colorizer written in perl to reformat and colorize the output from many different programs. Mainly, however, I use it to make the output from "tail -f" commands more readable. The base perl script I use is based on "colorlogs.pl" available from the... (1 Reply)
Discussion started by: rcsteiner
1 Replies

7. Shell Programming and Scripting

Capture all error message in Log file and send the Log file by email

Hi I have a requirement to write a script to capture all errors in a Logfile and send the file in email. If there is any error occurred the subject of email will be ERROR , If there are no error occurred the subject of email will be SUCCESS. So I created a Log file and put the Appropriate... (2 Replies)
Discussion started by: dgmm
2 Replies

8. Shell Programming and Scripting

how can i pick the latest log file as per below

in the below .. i want to pick the latest logfile which is having JPS.PR inside.. that means i want particularly "spgport040408041223.log:@@@@@@@@ 04:13:09 Adding: JPS.PR." which is latest among these.. is it possible to compare the current time with logfile time ? reptm@xblr0758rop>... (4 Replies)
Discussion started by: mail2sant
4 Replies

9. UNIX for Dummies Questions & Answers

capture shell output in cron entry

Hey all, I'm running scripts from cron and I want to capture the output from the 1 file handle. Ex. * * * * * /test.sh 1>test.log. I also want to append a formatted date to the file. * * * * /test.sh 1>test.log_date +%m%d%y but I keep keep getting the output as if I had just added the date... (5 Replies)
Discussion started by: steve72
5 Replies

10. Shell Programming and Scripting

capture nohup log file

Hi, I am running my script using nohup, but I am not able to capture the log file for that process could naybody please help... Here is what I am doing.... nohup ./script & 1>/home/user1/log.txt but I am not able to capture the log.....Is there anyother way I can capture the log... (2 Replies)
Discussion started by: mgirinath
2 Replies
Login or Register to Ask a Question