Luciano
Luciano

Reputation: 11

How to 'grep' a continuous log file stream with 'tailf' and when the needed string is obtained, close/break the 'tailf' automatically?

into a bash script, I need to grep a contiuous log streaming and when the proper string is filtered, I need to stop the 'tailf' command to move ond with other implementations.

The common command that works is:

tailf /dir/dir/dir/server.log | grep --line-buffered "Started in"

after the "Started in" line is gathered, I need to break down the "tailf" command.

All this stuff into a bash script.

Upvotes: 1

Views: 2068

Answers (2)

Luciano
Luciano

Reputation: 11

Figured out...

tailf /dir/dir/dir/server.log | while read line
do
    echo $line | grep "thing_to_grep"
        if [ "$?" -eq "0" ]; then
        echo "";echo "[ message ]";echo "";
        kill -2 -$$
    fi  
done

$$ is the PID of the current shell, in this case associated to the "tailf" command.

Upvotes: 0

georgexsh
georgexsh

Reputation: 16624

use grep -m1, it means return the first match then stop:

-m num, --max-count=num
Stop reading the file after num matches.

tailf /dir/dir/dir/server.log | grep -m1 "Started in"

Upvotes: 2

Related Questions