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.

alexus
  • 13,112
  • 32
  • 117
  • 174

1 Answers1

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.

Nehal Dattani
  • 581
  • 2
  • 10