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'.
|