If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Cronlog check script

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-01-04, 00:52
xor xor is offline
Registered User
 
Join Date: Jun 2003
Posts: 39
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
Reply With Quote
  #2 (permalink)  
Old 04-01-04, 03:16
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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.
Reply With Quote
  #3 (permalink)  
Old 04-12-04, 21:14
xor xor is offline
Registered User
 
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


Quote:
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'.
Reply With Quote
  #4 (permalink)  
Old 04-13-04, 06:01
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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.
Reply With Quote
  #5 (permalink)  
Old 04-13-04, 21:19
xor xor is offline
Registered User
 
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
Reply With Quote
  #6 (permalink)  
Old 04-15-04, 03:16
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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.
Reply With Quote
  #7 (permalink)  
Old 04-18-04, 21:57
xor xor is offline
Registered User
 
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
Reply With Quote
  #8 (permalink)  
Old 04-19-04, 03:25
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
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.
Reply With Quote
  #9 (permalink)  
Old 04-21-04, 20:47
xor xor is offline
Registered User
 
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
Reply With Quote
  #10 (permalink)  
Old 04-22-04, 05:15
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
All the variable in the script are locals to the awk script : No side effects
__________________
Jean-Pierre.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On