Solved match a pattern in a file and exit immediately

I want to monitor a log file for a particular change, so, I was thinking I would do:

Code:
tail -f <LOG_FILE> | grep -m 1 "SOME PATTERN"


However, while grep does exit, the tail process is left open. So, what I ended up doing was using a named pipe, but this is only marginally better, because it doesn't work without the while loop:
Code:
while [ 1 ]; do
            grep -q -m 1 "SOME PATTERN" <NAMED_PIPE>
            if [ $? -eq 0 ]; then
                break
            fi

            # try to be somewhat less resource intensive
            sleep 1
        done

I should also state that the named pipe doesn't solve the problem, I have to get the PID and then ultimately kill it which I do (outside of the code shown here).

This is a bit hackish and I was hoping for a better way to achieve this. The while loop is also not efficient, so I'd like to remove that if possible.
 
Thanks, this is a minor change I did:

Code:
/bin/sh -c "echo \$\$; exec tail -f $_LOG_FILE" | {
        IFS= read _TAIL_PID
        grep -q -m 1 -- "New IP Address"
        kill -s PIPE $_TAIL_PID
    }

I replaced single quotes for double quotes and escaped the dollar sign to get the PID.
 
Back
Top