2

Need to create a script where I want to take the last few lines of the log file and check that the last line with Start Date contains the date of today and the time 17:00:xx and is followed by xxxxx rows updated. and Commit complete. The script will check if the current date and time (17) has 'rows completed' string or not. If not trigger a mail.

Below is the format of the log file.

Start Date, Tue Jan 28 17:00:01 +03 2019
23468 rows updated.
Commit complete. 
--------------------------------------------
Start Date, Tue Jan 28 21:00:01 +03 2019
13369 rows updated.
Commit complete. 
--------------------------------------------
Start Date, Tue Jan 29 17:00:01 +03 2019
33099 rows updated.
Commit complete.
--------------------------------------------
Start Date, Tue Jan 29 21:00:01 +03 2019
21970 rows updated.
Commit complete.

Below is the code that I tried :

TIME=$(date '+%H%M')

l=$(cat /Data/v3/currlog.txt | grep "rows updated." | wc -l)

if [ $l -eq 0 ] && [ $TIME -eq 1700 ] ; then
echo "Please check if the records are updated" |  mailx -S  "check the rejection script" xyz@gmail.com

fi

But the above script is not solving my purpose.

TO CLARIFY MY NEED :

Suppose the log file(currlog.txt) is updated today as below :

Start Date, Sat Feb  2 05:00:01 +03 2019
2708722 rows updated.
Commit complete.

Now I need that If I schedule the script to run at 6. It should check if the rows were updated at 5 or not. If not it should trigger the mail.

The log file gets updated everyday in same format.

  • 2
    I don't understand what you are doing with $TIME. Do you only want the script to work if it is exactly 17:00? So it should only work during one minute of the day? – terdon Feb 2 at 11:01
  • @terdon.. The log file updates the rows twice in a day. once at 5pm and then at 9pm. if I dont specify the $time in the script then how the script when executed suppose at 6pm will check if the 'rows updated.' string is present at 17pm for the day or not. I am new in writing script can you please guide me. – Aishwarya Mishra Feb 2 at 11:25
  • 1
    As written, your script will only work at exactly 17:00 in the afternoon. So at 17:00, if the file /Data/v3/currlog.txt contains at least one instance of the string "rows updated.", then it will send an email. I think what you want is to check the number of lines updated at 17:00, is that right? What output do you expect if you run the script on the input you show? I just don't get what you are trying to do. Please edit your question and explain exactly what you need. – terdon Feb 2 at 11:32
  • Are there any other entries in your log file or it only contains those 3 lines per each run and nothing else? – DevilaN Feb 2 at 12:23
  • @DevilaN ..it just contains those 3 lines per each run and nothing else – Aishwarya Mishra Feb 2 at 14:06
0

I've taken the approach of extracting the last three lines from the log, and then checking that today's timestamp is included in those three lines.

#!/bin/bash

today_time=$(date -d 'today 5 PM' '+%a %b %e %H:%M')

if ! tail -n3 /Data/v3/currlog.txt | grep "$today_time" &> /dev/null ; then
        echo "Please check if the records are updated" |  mailx -S  "check the rejection script" xyz@gmail.com
fi

The GNU date command is used to get the date string at 5 PM on the same day. The specified format will output the date as 'Sat Feb 2 17:00' which can be searched against the timestamp in the log file.

  • it should also check if the any rows have been updated or not as in case of any failure of rows updation, the date will obviously will be there. so it should trigger the mail in case there is no record updated. – Aishwarya Mishra Feb 2 at 18:43
  • @AishwaryaMishra You haven't shown any logs of that type in your question. The only information I have about your situation is what you have included in your question. – Haxiel Feb 3 at 7:37
0

Checked with below command and it worked fine

 d=`date +"%d"`
 m=`date +"%b"`
y=`date +%Y`
awk -v d="$d" -v m="$m" -v y="$y" '$4 ==  m && $5 == d && $NF == y && $6 ~ /^17/ {x=NR+2}(NR<=x){print }' filename

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.