find a keyword while tailing a log and then do X

By : Codrguy
Source: Stackoverflow.com
Question!

Can i have a script running in a Linux environment that tails a log file and then as soon as it sees a certain keyword ( say "EndAPP") it runs another script? If so how ?

By : Codrguy


Answers

you can use this code...

#!/bin/bash

FILE_TO_TAIL=/file/to/tail
PATTERN="EndAPP"
APP_TO_EXEC="app.bin"


tail -1f ${FILE_TO_TAIL} |
while read -r line
do
    if [ ${PATTERN}=${line} ]; then
        ./${APP_TO_EXEC}
    fi
done
exit
By : alsotoes


You could use File::Tail

use File::Tail;

my $file=File::Tail->new("/some/log/file");
while (defined(my $line = $file->read)) {
    system("/path/to/second/script") if($line =~ /EndApp/) ;
}
By : Toto


This video can help you solving your question :)
By: admin