1

I must read the log until I receive “Server Startup”.

When I get the string “startup”, I must exit the tail command and proceed through the rest of the script operations.

Now the syntax is:

cd /opt/path/path/path
LOG_FILE=$(ls -ltr | tail -1 |awk '{print $9}')
tail -f $LOG_FILE

Thanks in advance

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
Luca
  • 41
  • 4
  • using `ls -lrt` is not a good idea to find latest file in directory. What is your latest file name contains space ? something like `"latest file"` – P.... Sep 19 '16 at 10:42

1 Answers1

4
stdbuf -oL tail -f log.file | stdbuf -iL awk '/startup/ {print "Match found";exit}'

This will check for string startup and when it is found, it will print "Match found" and then exit from log monitoring. stdbuf to provide line buffered to tail and awk.

P....
  • 17,421
  • 2
  • 32
  • 52
  • I recommend to use `stdbuf` to force `tail` to write line buffered and `awk` to read stdin line-buffered. Otherwiese the script would have to wait until the IO buffer get's flushed. Like this: `stdbuf -oL tail -f log.file | stdbuf -iL awk ...` – hek2mgl Sep 19 '16 at 10:43
  • 1
    @hek2mgl ,thank you. Point noted. – P.... Sep 19 '16 at 10:46
  • thanks for your availability – Luca Sep 19 '16 at 13:52