![]() |
|
|
grep unix.com with google
|
|||||||
| Forums | Register | Blog | Man Pages | Forum Rules | Links | Albums | FAQ | Our Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Emergency UNIX and Linux Support !! Help Me!! Post your urgent questions here for highest visibility. Posting a new thread to this forum requires Bits. We monitor this forum to help people with emergencies, but we do not guarantee response time or answers. This forum is "best effort" only. Members who reply to posts here receive a bonus of 1000 Bits per reply. |
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
||||
|
Code:
ReadLines=0 while [ true ] do TMPVAR=$(date +%s) Totallines=$(wc -l /var/log/YOU.log ) Totallines=$((Totallines-ReadLines)) tail -n $Totallines /var/log/YOU.log | grep -A 50 'PATTERN' > /tmp/$TMPVAR.tmpfile if grep 'username' /tmp/$TMPVAR.tmpfile ; then mail -s failure xxxxx@yyy.com < /tmp/$TMPVAR.tmpfile fi rm -rf /tmp/$TMPVAR.tmpfile sleep 900 done Some concept .. You need to just check with syntax to suit your system. Basically what I am trying While [ true ] ---> infinate loop TMPVAR ... just to create tmp file... Checking number of lines now and subtracting from what we have previously read. So that we dont want to send error which we have seen already .. Tail -n ... read only last lines ... what we have not read from previous run .. And check patten and get lines matching above matching pattern. Then again grep in the 50 lines from PATTERN which are in tmp file .. to see if username is available or not .. if Available then mail .. else ... Nothing .. Cleanup: remove all tmp file etc .. and sleep for 900 sec 15 mins .. Better place to put this check is in cron: Incase you decide to put in cron removing while do ... then push this number to some tmp file .. Like records read and etc .. Also make note of more details in the same tmp where number of records are saved.. details like log file creation date etc to reset ReadLines variables incase the log gets rotated .. truncated etc ... When you change PATTERN and username for your system .. Please add proper escape character wherever applicable |
|
|||
|
1. Use crontab to run every fifteen minutes, key "man crontab" in shell.
2. This script may work <script.sh <your text to grep>: #!/usr/bin/ksh thelog="/your/local/logpath" user=$1 lines="50" run () { num1=$(cat -n $thelog |grep $1 |awk '{print $1}' |head -1) num2=$(expr $num1 - $lines) if [ $num1 -gt 50 ] then results=$(cat -n $thelog |sed -n "$num2,${num1}p") else results=$(cat -n $thelog |sed -n '1,50p') fi echo "$results" |mailx -s "blabla" yourmailbox@nospamplease.com } run $user; |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Finding a word at specific location in a string | swapnil.nawale | Shell Programming and Scripting | 1 | 09-14-2009 09:49 AM |
| Word count of lines ending with certain word | warlock129 | Shell Programming and Scripting | 8 | 08-30-2009 04:37 AM |
| finding the number of occurence of a word in a line | priyanka3006 | Shell Programming and Scripting | 9 | 06-18-2009 08:55 AM |
| need help with finding a word in file's contents | manmeet | Shell Programming and Scripting | 6 | 10-01-2008 03:21 PM |
| Finding a word in a file | smr_rashmy | Shell Programming and Scripting | 10 | 02-13-2008 02:02 AM |