Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to write a script that will ssh into a box, start a processes that needs to be monitored (by a human) for success or failure then move onto the next box and repeat.

I've been trying to use tail or tail -f on the log that the process writes to, but I've yet to find a way to get the script to read keyboard input while the tail -f is running.

Ideally I'd like to tail -f the log until I see the process was successful, press a key and have the script move on, OR press another key if the startup errors out and exit the script.

Is this possible?

So far I've only come up with:

    #!/bin/bash
    kb=""
    until [ "$kb" == "n" ]; do
    tail /var/log/java-app.log
    read -t 1 kb
    done

It mostly works, but it's klunky. I'm starting up java apps and there are pauses so occasionally the same 10 lines are repeated and I have to hit enter to have the n acknowledged. I'd really like to use tail -f and still read (ideally 2) different vars from the keyboard. Is this possible?

{edit}
I'm actually thinking that

tail -f /var/log/java-app.log | tee > ( grep -q "YADA YADA" )

Is my best bet. I can just Ctrl+C if I find errors.

share|improve this question
    
You don't seem to be observing that your program might already have exited while watching the log. In a similar situation (I had to scan the output from the process as well as a log) I ended up writing a program that looped while 1) scanning for keyboard input 2) write out output and log to stdout for visual check (suppressing non-interesting lines). This was extended over time with 3) automated scanning for patterns in log and output. I am not sure if you can do that as easily with shell programming. –  Anthon Jun 2 '13 at 6:53
    
Well, that's the thing. I need those non-interesting lines to check for errors. Basically I'm starting up several JVMs across about 10 servers. I need to see all of the output. When I see it's "started successfully" I want it to continue onto the next JVM or server. If I see errors I want the whole script to die. –  kadamwolfe Jun 2 '13 at 16:03
    
Yeah I understand that, but e.g. you repeat the tail and there might be to many lines passed already, so you miss things, or not enough, so you see things multiple times. In my program I record the size of the log, and display additional bytes/lines, after checking for keyboard events. –  Anthon Jun 2 '13 at 16:10
    
Agreed. Which is why I am leaning toward tail -f /var/log/java-app.log | tee >( grep -q "YADA YADA" ) instead. Grep will exit when it finds the match I am looking for and the script moves to the next step. If I see it failing I can ctrl+C. –  kadamwolfe Jun 2 '13 at 16:28

1 Answer 1

#! /bin/bash

echo "Starting tail in the background..." 
tail -f input &
pid=$!
trap "kill $pid" EXIT
while true; do
        read kb
        test n = "$kb" && break
done
share|improve this answer
    
this might work if I put in two [test] instances for kb. I need one to continue, and one to kill the whole script. –  kadamwolfe Jun 2 '13 at 16:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.