0

I'm trying to create a shell script that would look for ERROR inside of /var/log/jbossas/standalone/server.log, such as following:

# grep ERROR /var/log/jbossas/standalone/server.log
10:36:37,530 ERROR [class X] (ajp-/192.168.X.X:8009-20) THIS IS A TEST
#

script will be executed via cron, so it must look for error within certain time frame only, otherwise there could be duplicate emails every time it executes.

this is my current solution to this:

# crontab -l
@hourly     grep ERROR /var/log/jbossas/standalone/server.log | grep `date +%H --date="1 hour ago"`
# 

unfortunately this solution limits me to run on hourly bases only.

I thought I'd ask question here and see if I can find better solution then mine.

1
  • Is the log being rotated via logrotate?
    – Andy
    Aug 21, 2015 at 9:30

1 Answer 1

0

The best approach would be to rotate or move the log file BEFORE you process them. something like,

#cp /path/to/log /path/to/moved_log/log.timestamp && echo > /path/to/log && grep ERROR /path/to/moved_log/log.timestamp

Alternatively you can do not-so-preferred approach,count the number of lines in existing log file and process it. Store the number somewhere and use tail to get all the lines from log from last processed line.

First Execution:

# wc -l /path/to/log > /var/tmp/log.num # grep ERROR /path/to/log

Subsequent execution (inside cron):

tail -n + $(cat /var/tmp/log.num) | grep ERROR wc -l /path/to/log > /var/tmp/log.num

I hope someone will come-up with better approach for record keeping method I have suggested above.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .