LinuxQuestions.org
Review your favorite Linux distribution.
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 11-17-2013, 10:40 AM   #1
linson_85
Member
 
Registered: Oct 2008
Location: Latitude= 9.9722, Longitude= 76.378
Distribution: Open VZ,fedora & Centos
Posts: 47

Rep: Reputation: 0
Output only the last 2 minute's of log


Hello,

I want to list the last 2 minutes entry of a log file. For instance /var/log/messages

Is there any way we can achieve this ? Means it should only show the last 2 minutes entry written in the logs.
 
Old 11-17-2013, 11:24 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530Reputation: 3530
Probably an easier way but until then:
Code:
RES=($(/bin/date "+^%b.%e.%H:%M" --date="2 minutes ago"; /bin/date "+^%b.%e.%H:%M" --date="1 minutes ago")); 
RES=${RES[@]}; RES=${RES// /|}; egrep "(${RES})" /var/log/messages
 
Old 11-17-2013, 11:24 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Have a look at this:
Code:
#!/bin/bash

CURRENT=$( date '+%s' )
TARGET=$(( $CURRENT - 2*60 ))
LOGFILE="/var/log/messages"

while read MONTH DAY TIME REST
do
  TIMESTAMP=$(date --date "$MONTH $DAY $TIME" '+%s')
  [[ "$TIMESTAMP" -ge "$TARGET" ]] && echo "$MONTH $DAY $TIME $REST"
done < $LOGFILE
I'm assuming that the log file uses this format: Month Day Time Message / Nov 17 17:24:30 ..........
 
Old 11-17-2013, 12:03 PM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
Seeing that you have Fedora listed in your profile, on distributions using systemd with journald it is as easy as
Code:
journalctl --since=-00:02:00
 
3 members found this post helpful.
  


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
(Bash) Redirect all output from script to all.log and copy of errors to err.log hmsdefender Programming 5 03-05-2010 02:52 PM
Clean Up Log - Search for Pattern in Log file and Output result bridrod Linux - Newbie 10 01-05-2010 10:49 AM
something writing messages to log every minute jkcunningham Linux - General 3 02-17-2005 07:50 AM
log the output ilnli Linux - General 2 01-06-2005 02:41 PM
mail log reporting error every minute kwingate Fedora 0 10-25-2004 10:07 PM


All times are GMT -5. The time now is 10:39 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