get last 5 minutes' log from log file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting get last 5 minutes' log from log file?
# 1  
Old 07-19-2006
get last 5 minutes' log from log file?

Hi all, I have tried to figure out a way to automatically get the last 5 minutes' log content from log file, at first, my thoughts like this,

sed -n "/ $(date +\%R -d "-5 min")/,$"p syslog > newfile,

but quickly I found it did not work, say I have a syslog file as following,

Jul 19 18:15:23 ........
Jul 19 18:15:28 ........
Jul 19 18:17:11 ........
Jul 19 18:18:11 ........

if the script run at 18:21,18:21 minus 5minutes is 18:16,since there is no "18:16" in logfile, sed will find no match, thus there will be nothing in the newfile", although actually it should have!

I know there will be solution around the corner, but how? Thanks!
# 2  
Old 07-19-2006
Well since there are only 5 minutes you are interested in:

in bash

Code:
for (( i = 5; i >=0; i-- )) ; do
     grep $(date +%R -d "-$i  min") syslog >> newfile
done

This User Gave Thanks to reborg For This Post:
# 3  
Old 07-19-2006
Hi reborg, thank you for your reply, Smilie

I am just wondering is there any other better solution other then using "grep",

If the syslog file is super large, doing loop and grep maybe not a very good solution. Smilie

Last edited by fedora; 07-19-2006 at 04:48 PM..
# 4  
Old 07-19-2006
Try this one
Code:
#!/bin/sh

e=
for (( i = 5; i >= 0; i-- )) ; do
    e='-e /'`date +\%R -d "-$i min"`'/p '$e
done

$(sed -n $e syslog > newfile)

This script generates commands like
Code:
sed -n -e /23:40/p -e /23:39/p -e /23:38/p -e /23:37/p -e /23:36/p -e /23:35/p  syslog > newfile

# 5  
Old 07-19-2006
Well you could use egrep and multiple patterns to only search once.

assign each of the last 5 minutes to a variable and do

egrep "var1|var2|var3|var4|var5" syslog > newlog

or just take the last line of the previous extracted log.

Code:
var=$(tail -1 last_log_file)
sed -ne "/$var/"',$p' syslog > newlog

# 6  
Old 07-19-2006
AH! much better, thanks, you guys! Smilie Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Copy last 30 minutes' contents from a log file

Hi Guys, I am writing a bash script to capture the last 30 minutes's contents from log file to a new file. This job is a scheduled job and will run every 30 minutes. The log file is db2diag.log in DB2. I am having difficulties copying the last 30 minutes's contents. Can someone please help me.... (4 Replies)
Discussion started by: naveed
4 Replies

2. Shell Programming and Scripting

Grep a log file for the last 5 minutes of contents every 5 minutes

Hi all, System Ubuntu 16.04.3 LTS i have the following log INFO 2019-02-07 15:13:31,099 module.py:700] default: "POST /join/8550614e-3e94-4fa5-9ab2-135eefa69c1b HTTP/1.0" 500 2042 INFO 2019-02-07 15:13:31,569 module.py:700] default: "POST /join/6cb9c452-dcb1-45f3-bcca-e33f5d450105... (15 Replies)
Discussion started by: charli1
15 Replies

3. Shell Programming and Scripting

Grep last 2 minutes log only

I have newbie, which i use for checking last one hours log file, but i want to check 2 minutes log and discard old log, only match current time with last 2 minutes. Ex log. 2018-07-03 20:09:17 2018-07-03 20:05:17 2018-07-03 20:05:18 2018-07-03 20:05:20 2018-07-03 20:06:22 2018-07-03... (5 Replies)
Discussion started by: ooilinlove
5 Replies

4. Shell Programming and Scripting

Need help in getting the Last 30 minutes logs from the Log File

I have a log file with the below contents : log_file_updated.txt : Jul 5 03:33:06 rsyslogd: was Jul 5 03:33:09 adcsdb1 rhsmd: This system is registered. Sep 2 02:45:48 adcsdb1 UDSAgent: 2015-07-05 04:24:48.959 INFO Worker_Thread_4032813936 Accepted connection from host <unknown>... (3 Replies)
Discussion started by: rahul2662
3 Replies

5. Shell Programming and Scripting

Script to search for a pattern in 30 minutes from a log file

Hello All, I have to write a script which will search for diffrent patterns like "Struck" "Out of Memory" , etc from a log file in Linux box's. Now I will be executing a cron job to find out the results by executing the script once in every 30 minutes. suppose time is 14-04-29:05:31:09 So I... (3 Replies)
Discussion started by: Shubhasis Mathr
3 Replies

6. Shell Programming and Scripting

Scan of log file in Linux for entries in last 15 minutes for matching a pattern

Is there any way I can do scan of log file in Linux, where the log file entries for last 15 minutes can be searched for a particular pattern. The log file entries are in below format. 2014-01-27T23:08:53.924-0500 LDAP authentication error 2014-01-27T23:08:53.934-0500 LDAP authentication... (4 Replies)
Discussion started by: anandrudran
4 Replies

7. Shell Programming and Scripting

Grep last 30 minutes log only

I have below command, which i use for checking last two hours log file, but i want to check 30 minutes log and discard old log, only match current time with last 30 minutes. Command i am using. This below attach log file format is for this week, but sometime it got change in time of log, so i need... (6 Replies)
Discussion started by: learnbash
6 Replies

8. Shell Programming and Scripting

Log File Creations for every 60 minutes

Hi All, Below script will make a copy of the existing log file with the then timestamp details. I am looking to create a copy of the existing log file for every 60 minutes and when the file limit reaches to 5, the 6th copy should overwrite the first backedup file which means all the time it... (3 Replies)
Discussion started by: Upendra Bestha
3 Replies

9. Shell Programming and Scripting

Finding errors in log file only in last 10 minutes

Hi there, I have a log file that I need to check every 10 minutes to find if a specific error exists but only in that 10 minute period. The reason is that the log is quite large, and will frequently contain these errors, so I only want alerting if it in the last 10 minutes - I don't want... (3 Replies)
Discussion started by: paul_vf
3 Replies

10. Shell Programming and Scripting

Grepping the last 30 minutes of a log file...

I need to know if anyone can assist me on how to grab the last (we'll just say "x" minutes) of a log file. How do you tell the grep command without specifying an exact window of time? (So relative instead of absolute.) Thanks, Jon (2 Replies)
Discussion started by: jtelep
2 Replies
Login or Register to Ask a Question