Results 1 to 10 of 10
  1. #1
    Join Date
    Jun 2003
    Posts
    39

    Unanswered: Cronlog check script

    Hi ,

    I am trying to write a script that will check the cron log file and email the status if the cron job has run completed execution or failed.. This is what the cron process appends to the cron log once the job has executed

    > CMD: /u/cm_clr.sh
    > root 3737 c Tue Mar 30 18:11:00 2004

    < root 3737 c Tue Mar 30 18:30:23 2004

    I am currently in a problem here where I want to grep "/u/cm_clr.sh" then also go to next line and grep the date and pid line so when i try

    #cat log1 | grep /u/cm_clr.sh Tue Mar 30 18:11:00 2004

    it does not output both lines.. any other way of achieving this would be very welcome as all i want is to get notified when cron job started and when it completeted.. for every job

    xor

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Use awk instead of grep.
    The following command, print the CMD line and the next line
    Code:
    awk '/^> CMD:/ {print; getline; print}' log1
    To resolve your problem, you can do something like this :
    Code:
    tail -f cron_log | \
    awk -v DEST=dest_of_mail '
    
    function Mail(dest, subject, text    , mail) {
       mail = "echo \"" text "\" | mail -s \"" subject "\" " dest;
       system(mail);
    } 
    
    function Mail_Start(pid    ,text) {
       text =      "Job start : " Start[pid] "\n"
       text = text "     User : " User[pid]  "\n"
       text = text "      Cmd : " Cmd[pid]   "\n"
       text = text "      Pid : " pid        "\n";
       Mail(DEST, "Cron - Job start", text);
    }
    
    function Mail_End(pid    ,text) {
       text =      "Job End : " End[pid]   "\n"
       text = text "Started : " Start[pid] "\n"
       text = text "   User : " User[pid]  "\n"
       text = text "    Cmd : " Cmd[pid]   "\n"
       text = text "    Pid : " pid        "\n";
       Mail(DEST, "Cron - Job end", text);
    }
    
    /^> CMD:/ { 
       command = $3;
       getline;
       pid = $3;
       Cmd[pid]  = command;
       User[pid]  = $2;
       Start[pid] = $5 " " $6 " " $7 " " $7 " " $8 " " $9;
       Mail_Start(pid);
       next;
    }
    
    /^< / {
       pid = $3;
       if (pid in Cmd) {
          End[pid] = $5 " " $6 " " $7 " " $7 " " $8 " " $9;
          Mail_End(pid);
          delete Cmd[pid];
          delete User[pid];
          delete Start[pid];
          delete End[pid];
       }
    }
    '
    For every started or completed job, a mail is send to the user 'DEST'.
    Jean-Pierre.

  3. #3
    Join Date
    Jun 2003
    Posts
    39

    Cronlog check script

    Hi aigles

    I have tried your script and it works fine only there is a slight issue with the script respawning iself also several instances of "tail" command has been spawned. Would there be any reason behing awk doing this, also there are some instances of the script that has the /etc/cron process as its PPID?? Any suggestions to fix this.

    Server011# ps -ef | grep cron_chk.awk
    root 3988 10083 0 Apr-10 ? 00:00:00 sh -c /usr/cron_chk.awk
    root 3990 3988 0 Apr-10 ? 00:00:00 sh -c /usr/cron_chk.awk
    root 29380 10083 0 Apr-09 ? 00:00:00 sh -c /usr/cron_chk.awk
    root 11381 10083 0 Apr-11 ? 00:00:00 sh -c /usr/cron_chk.awk
    root 11385 11381 0 Apr-11 ? 00:00:00 sh -c /usr/cron_chk.awk
    root 29387 29380 0 Apr-09 ? 00:00:00 sh -c /usr/cron_chk.awk
    root 4713 10083 0 03:00:00 ? 00:00:00 sh -c /usr/cron_chk.awk
    root 4717 4713 0 03:00:00 ? 00:00:00 sh -c /usr/cron_chk.awk
    root 21660 10083 0 Apr-12 ? 00:00:00 sh -c /usr/cron_chk.awk
    root 21664 21660 0 Apr-12 ? 00:00:00 sh -c /usr/cron_chk.awk
    root 25533 24174 2 13:41:23 ttyp0 00:00:00 grep cron_chk.awk

    Server011# ps -ef | grep 10083
    root 3988 10083 0 Apr-10 ? 00:00:00 sh -c /usr/cron_chk.awk
    root 29380 10083 0 Apr-09 ? 00:00:00 sh -c /usr/cron_chk.awk
    root 11381 10083 0 Apr-11 ? 00:00:00 sh -c /usr/cron_chk.awk
    root 4713 10083 0 03:00:00 ? 00:00:00 sh -c /usr/cron_chk.awk
    root 21660 10083 0 Apr-12 ? 00:00:00 sh -c /usr/cron_chk.awk
    root 10083 1 0 Apr-08 ? 00:00:00 /etc/cron
    root 25556 24174 2 13:42:15 ttyp0 00:00:00 grep 10083

    Server011# ps -ef | grep tail
    root 3992 3991 0 Apr-10 ? 00:00:02 tail -f /usr/lib/cron/log
    root 29391 29389 0 Apr-09 ? 00:00:02 tail -f /usr/lib/cron/log
    root 11387 11386 0 Apr-11 ? 00:00:01 tail -f /usr/lib/cron/log
    root 21675 21674 0 Apr-12 ? 00:00:01 tail -f /usr/lib/cron/log
    root 4719 4718 0 03:00:00 ? 00:00:00 tail -f /usr/lib/cron/log

    Xor


    Originally posted by aigles
    Use awk instead of grep.
    The following command, print the CMD line and the next line
    Code:
    awk '/^> CMD:/ {print; getline; print}' log1
    To resolve your problem, you can do something like this :
    Code:
    tail -f cron_log | \
    awk -v DEST=dest_of_mail '
    
    function Mail(dest, subject, text    , mail) {
       mail = "echo \"" text "\" | mail -s \"" subject "\" " dest;
       system(mail);
    } 
    
    function Mail_Start(pid    ,text) {
       text =      "Job start : " Start[pid] "\n"
       text = text "     User : " User[pid]  "\n"
       text = text "      Cmd : " Cmd[pid]   "\n"
       text = text "      Pid : " pid        "\n";
       Mail(DEST, "Cron - Job start", text);
    }
    
    function Mail_End(pid    ,text) {
       text =      "Job End : " End[pid]   "\n"
       text = text "Started : " Start[pid] "\n"
       text = text "   User : " User[pid]  "\n"
       text = text "    Cmd : " Cmd[pid]   "\n"
       text = text "    Pid : " pid        "\n";
       Mail(DEST, "Cron - Job end", text);
    }
    
    /^> CMD:/ { 
       command = $3;
       getline;
       pid = $3;
       Cmd[pid]  = command;
       User[pid]  = $2;
       Start[pid] = $5 " " $6 " " $7 " " $7 " " $8 " " $9;
       Mail_Start(pid);
       next;
    }
    
    /^< / {
       pid = $3;
       if (pid in Cmd) {
          End[pid] = $5 " " $6 " " $7 " " $7 " " $8 " " $9;
          Mail_End(pid);
          delete Cmd[pid];
          delete User[pid];
          delete Start[pid];
          delete End[pid];
       }
    }
    '
    For every started or completed job, a mail is send to the user 'DEST'.

  4. #4
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    You do you run the script ? It must not be include in yours cron jobs.
    It must be run only once since it scan the cron log pemanently.
    Jean-Pierre.

  5. #5
    Join Date
    Jun 2003
    Posts
    39

    Cronlog check script

    I ran the script in the background. there is no cron entry in the crontab file for the script...

    Server01#./cron_chk.awk &
    1027
    Server01#

    Also why has /etc/cron to spwan the script when there is not refrence to /etc/cron??

    xor

  6. #6
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    I don't understand how you can get this result.
    Did you run the script from the shell prompt or from a script ?
    In that last case verify that the script is not called by another script (directly or indirectly)
    Jean-Pierre.

  7. #7
    Join Date
    Jun 2003
    Posts
    39

    Cronlog check script

    Hi ,

    How do I run the script so that it stays in memory as a process and does not exit when I exit the terminal. The problem was that I ran it as a cron job as a result cron was spwaning new instances as the scheduled time.

    xor

  8. #8
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    You can use nohup :

    nohup ./cron_chk.awk &


    Another solution is to start your script as a deamon at boot time.
    The method is dependant of your Unix flavor (/etc/inittab, /etc/rc,...) , contact your system manager.
    Jean-Pierre.

  9. #9
    Join Date
    Jun 2003
    Posts
    39

    Cronlog check script

    Hi,

    I need to make sure that the variables in this script are not set outside this script. How do I check that all the variables in this script are set only within this script and not applied globally as this could have disasterous effects on other applications and scripts

    Xor

  10. #10
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    All the variable in the script are locals to the awk script : No side effects
    Jean-Pierre.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •