Create a script to check the date, time and string and trigger a mail

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












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.










share|improve this question



















  • 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















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.










share|improve this question



















  • 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













2












2








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.










share|improve this question
















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.







shell-script shell command-line mail-command






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 2 at 21:23









Rui F Ribeiro

40.4k1479137




40.4k1479137










asked Feb 2 at 10:40









Aishwarya MishraAishwarya Mishra

143




143







  • 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












  • 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







2




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





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





@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




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





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





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





@DevilaN ..it just contains those 3 lines per each run and nothing else

– Aishwarya Mishra
Feb 2 at 14:06










2 Answers
2






active

oldest

votes


















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.






share|improve this answer























  • 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





share|improve this answer






















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f498267%2fcreate-a-script-to-check-the-date-time-and-string-and-trigger-a-mail%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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.






    share|improve this answer























    • 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














    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.






    share|improve this answer























    • 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












    0








    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.






    share|improve this answer













    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Feb 2 at 14:30









    HaxielHaxiel

    2,9051917




    2,9051917












    • 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

















    • 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
















    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






    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





    @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





    share|improve this answer



























      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





      share|improve this answer

























        0












        0








        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





        share|improve this answer













        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 2 at 18:18









        Praveen Kumar BSPraveen Kumar BS

        1,490138




        1,490138



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f498267%2fcreate-a-script-to-check-the-date-time-and-string-and-trigger-a-mail%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?