I am looking for getting the logs for the last n minutes. I tried a few options but most of them didn't work if there are no logs specified for a particular timestamp.
For example:
The one worked fine if current time is 2019-07-27 09:00:01
, and I look for the log 10 minutes ago till current time. It worked if there is a line in the log file 2019-07-27 08:50:01
, but it doesn't print anything if there is no line in logs at this timestamp.
for (( i = 10; i >=0; i-- )) ; do grep $(date +%R -d "-$i min") test.log
I also tried an awk option given in the thread Get logs for last 30 minutes
However it didn't work whenever there is a line in the log file that does not have a date timestamp in the beginning.
awk -vStartDate=`date -d'now-30 min' +%H:%M:%S` '{ if ($2 > StartDate) print $0}' test.log
Sample lines in my logfile are:
2019-07-27 01:28:35,8291 DEBUG [TestLogger] - Parameters: [100, IN%]
2019-07-27 01:28:35,8292 ERROR [TestLogger] - Query with error:
SELECT COUNT(*) FROM <table_name>
2019-07-27 01:28:35,8293 ERROR [TestLogger] - 'Connection reset' while executing statement
2019-07-27 01:28:35,8294 WARN [TestReportAction] - Count Error: TestReportAction - 'Connection reset' while executing statement
It should trim the line number as well while matching the time which is given after a comma ,
.
journalctl
you can do something likejournalctl --since '10 minutes ago'