2

I cant seem to make this work. This is the script.

tail -fn0 nohup.out | while read line; do

    if [[ "${line}" =~ ".*ERIKA.*" ]]; then

        echo "match found"
        break

    fi

done

echo "Search done"

The code echo "Search done" does not run even after a match has been found. I just want the rest of the code to be ran when a match has been found. I have not made it possible yet.

Sorry, I am new with log monitoring. Is there any workaround with this?

I am gonna run the script via Jenkins so, the code should be free flowing and should not require any user interaction.

Please help, thanks.

Erika
  • 65
  • 5

3 Answers3

3

You've got a couple of issues here:

  1. tail is going to keep running until it fails to write to its output pipeline, and thus your pipeline won't complete until tail exits. It won't do that until after your script exits, AND another line (or possibly 4K if buffering, see below) is written to the log file, causing it to attempt to write to its output pipe. (re buffering: Most programs are switched to 4K buffering when writing through pipes. Unless tail explicitly sets its buffering, this would affect the above behaviour).

  2. your regex: "${line}" =~ ".*ERIKA.*" does not match for me. However, "${line}" =~ "ERIKA" does match.

You can use tail's --pid option as a solution to the first issue. Here's an example, reworking your script to use that option:

while read line; do
    if [[ "${line}" =~ "ERIKA" ]]; then
        echo "match found"
        break
    fi
done < <(tail --pid=$$ -f /tmp/out)

echo "Search done"

Glenn Jackman's pkill solution is another approach to terminating the tail.

Perhaps consider doing this in something other than bash: perl has a nice File::Tail module that implements the tail behaviour.

There are many more questions related to this problem, you may find something you prefer in their answers:

Community
  • 1
  • 1
R Perrin
  • 471
  • 3
  • 8
  • Thank you for sharing all these great examples. – Erika Mar 20 '14 at 16:57
  • Good point about the regex not matching: @erika, in bash, do not quote regex-special character. Quoted characters lose their special meaning. – glenn jackman Mar 20 '14 at 16:59
  • It only loses its meaning if you use single quote. The original code i wrote still works, but yes, needless. – Erika Mar 21 '14 at 12:14
0

Here's one way, doesn't feel very elegant though.

tail -fn0 nohup.out |
while IFS= read -r line; do
    if [[ $line == *ERIKA* ]]; then
        echo "match found"
        pkill -P $$ tail
    fi
done

echo "Search done"
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • I think this is the simplest out of all the examples I saw. I think it only kills the tail process that was started by this current script. So, it might be unelegant but safe...I guess. – Erika Mar 20 '14 at 16:55
0

You can use awk to exit:

tail -fn0 nohup.out | awk '/ERIKA/{print "match found ", $0; exit}'
anubhava
  • 761,203
  • 64
  • 569
  • 643