Help with shell script
Hi,
need help in shell script especially in reading the log file. Have to grep a "error" from log file and copy to another file in a job which will be scheduled. During the next run of the job it should start looking for the error ignoring the last updated error on the file. if this is the last updated erorr 2013-04-29 21:44:11,086 [ERROR] com.ct.gd.web.tags.AdSelectTag - Failed to get Ad Content. then it should start looking from the log file from the time 2013-04-29 21:45 Confused at how to achieve this. Please help |
You simply should use another file to record the last timestamp or info that the error was took, and use it next time to know where to start reading again. In bash alone or awk, you could (a bit slowly) loop through all the lines everytime you run the script to get the next error.
Code:
#!/bin/bash |
I think you're in luck, in that the time stamp is basically in the proper ISO-8601 format. This means you should be able to just use standard string comparisons on them.
It would help a bit if you explained a bit more about exactly what the script is supposed to do; the way the code is structured can depend on it. But this is my proof of concept version: Code:
#!/bin/bash It might possibly be slow-performing on large datasets too, although since it only loops over the lines extracted from grep, it presumably won't have too much work to do. Still, if performance is a issue it may be better done in a language like perl. |
Quote:
Quote:
Code:
if [[ BASH_VERSINFO -ge 4 ]]; then Quote:
Code:
[[ $cur_stamp < "$lasttime" ]] |
Quote:
Are you saying that we can't trust the log's timestamps for some reason? I notice that I got the direction wrong in my script comment, though. Quote:
Quote:
And would it affect the current test in any case? |
Quote:
Quote:
It could be likely that strings inside quotes were handled differently than open strings on the second argument of < or >. |
Hi Konsolebox and David,
Thanks for the help. but my bash version is 3 so mapfile doesn't work on it. Also there are two type of error am looking as "Server Error" and Error. because "server error" doesn't have time stamp so the timestamp has to be taken from the next line. posting my code written which is very simple as i am a beginner. #!/bin/bash h1=`hostname` d1=`date` ABCLOGS=/path to logfile XYZlog1=/tmp for fname in $ABCLOGS do abc=$(basename $fname) f=${fname}/ABCLog.log.servererror var=`tail -1000 $f | grep -A 20 "Server Error" ` echo "$var" >/tmp/abclogcopy.txt done for fname1 in $XYZlog1 do xyz1=$(basename $fname1) f1=${fname1}/xyzlogcopy.txt var1=`cat $f1 | grep -c "Server Error" ` echo $var1 if [ "$var1" -ge "5" ] ; then echo "$var1" if [ ! -z "$var" ] ; then echo "$var error on $xyz on server $h1 @ $d1" | mailx -s "error on $xyz" <mail-addr> #break else echo "No matching error found on server $h1 @ $d1" >>/tmp/jobrun.txt fi else echo "Count was not found" fi done |
I'm fairly sure my general idea could still be made to work.
mapfile is just a convenience feature that can be replaced with a while loop, as konsolebox demonstrated. As for the second issue, an array may still be a way to go. Just make sure you grab all the important lines of output, then if an error line matches, you can simply grab the +1 entry and process that. I'm also considering the possibility of using sed, with a range match including the last updated line, as a way to limit the input. It would help if you could provide a longer example of the log text, and show us exactly what you want to get from it. As for your posted script, I'm not going to break it down in detail at this time, but here are a few general scripting points that should be addressed in it, at least: Don't Read Lines With For! Useless Use Of Cat $(..) is highly recommended over `..` Parameter substitution can replace basename [[..]] should generally be used for string/file tests, and ((..)) for numerical tests And please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques. Thanks. |
You should start by analyzing your requirement.
IF the requirement is to copy ERROR messages from the logfile to a different file without repetition then you can simply start a shell background process % nohup tail -f $LOGFILE | grep ERROR > $OUTFILE & |
All times are GMT -5. The time now is 07:47 PM. |