Console Beep On Error Line of a Log File

Ok folks , this is non-java :)

Today I had to watch a log file and alert some ppl when an error phrase seen on log file. This is a terrible jıb todo. First I run on my linux server:

#tail -f mylogfile | grep ERRORSTRING

this worked but put me in front of the screen. Why not my console beep when ERRORSTRING exists in the log file ? I need an application to beep when read a line from stdin. This is piece of cake in java but i did not want to load JVM for this purpose because of memory needs. So I warped to good old days and decided to write a C program to do this job.


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
char buf[1024];
int n;
while (fgets(buf, sizeof buf, stdin) != NULL) {
n = strlen(buf);
if (n > 0 && buf[n-1] == '\n') {
printf("\a");
printf("%s\n", buf);
}
}
}


and I compiled :
#cc beep.c -o mybeep

and later :

#tail -f mylogfile | grep ERRORSTRING | /home/hakan/utils/beep

and that's it , it worked ! I watch olympic games and when console beeps I called administration ppl :) There should be a lot of log watching utility but it is easier for me to write code than search google for utility programs and install them. Maybe this code can crash the server but it works for now , thanks in advance for comments.

Comments

Emre said…
Nice work! So easy! Best programmer is the laziest programmer :)

Popular posts from this blog

Connect via SSH without a password from MacOS to Linux with an SSHKEY

Read and Write Files in Java

Install Mac OS on windows laptop