2

So I have a script that starts a service and tails the log file until the service finally initializes. I used the following to achieve this: Linux: Block until a string is matched in a file ("tail + grep with blocking")

Now, I'd like my user to see the output of the tailed file when the tailing begins, so they can track the progress of the service. I know a few ways to ago about this, such as spawning a new window and piping all tail output to the new handle. This only works when the user is at the actual workstation but the script interacts remotely via SSH. So this won't work in my case.

I'm hoping someone here knows how to pipe the tail to the running window, meanwhile keeping the output of the tailed file to just a single line. I want to avoid spewing all the output to the scripts screen.

If I'm being too vague, let me know. I can post a screenshot to help better explain what I am trying to achieve.

Thank you

Community
  • 1
  • 1
user0000001
  • 2,092
  • 2
  • 20
  • 48
  • Which answer from there did you use? What do you mean when you say "pipe the tail to the running window, meanwhile keeping the output of the tailed file to just a single line" do you want the entire log shown or just the line you are searching for? – Etan Reisner Apr 15 '15 at 19:53
  • @EtanReisner The first answer. `grep -q 'PATTERN' <(tail -f file.log)`. And want the entire log piped to the window executing the script, but I'm hoping instead of spewing all the output, we can truncate it to just one single line. I wish I knew the correct terminology, I'm sure that would probably help you better understand what I am asking. – user0000001 Apr 15 '15 at 19:58
  • That's the part I don't understand. How do you expect to show an entire log file on a single line? Do you mean you want one line that keeps updating as new lines show up in the log? Like a "status line"? – Etan Reisner Apr 15 '15 at 21:16
  • @EtanReisner Cheers to that. That is exactly what I am looking for. – user0000001 Apr 16 '15 at 01:26
  • @EtanReisner I'm assuming we'd use `echo -ne \r`. I just don't know how to redirect the output. – user0000001 Apr 16 '15 at 01:30
  • 1
    Something like that would probably work though for a terminal I'd probably use terminfo sequences instead of just a raw carriage-return. You'll have to play with duplicating file descriptors before running `tail` I think to do this and while I could probably work it out this stuff always confuses me and takes longer than I think it should so I'm probably not the best person to help. – Etan Reisner Apr 16 '15 at 01:47
  • @EtanReisner Awesome. I'm going to attempt this with the information you've given me and hopefully come up with an answer. But thank you for the assistance thus far. Cheers. – user0000001 Apr 16 '15 at 02:24

1 Answers1

0

I'm not sure I'm picking up all your requirements clearly, is ssh a 100% necessity?

Here's a way you *could do it:

#if grep command succeeds then enter infinite loop to repeatedly post to netcat server for remote processing
if (grep -q 'PATTERN'); then
  while :
      tail -n 1 | nc -l 3333

And you can connect to it with:

nc <you_other_server's_ip> 3333
  • you spawn 1 tail process per line... shouldn't this work? (i can't try it now) : {grep -q 'PATTERN' && tail -f ;} | nc -l 3333 – Olivier Dulac Apr 16 '15 at 06:53