Alex Tverd
Alex Tverd

Reputation: 11

Bash script - How to make it show status every 3 seconds

I have a bash script and I want to make it to check up phrase in logs every 3 seconds and when it's done - show success message

I tried to do it so, but I don't see status updating every 3 seconds:

until tail -f /path/server.log | grep -i 'Server has started succesfully'
do
 echo Waiting ... '$date'
  sleep 3

done
  echo You are ready '$date'
done

Could you please explain to me what's the problem?

Upvotes: 0

Views: 290

Answers (2)

tripleee
tripleee

Reputation: 189936

The until loop waits for grep to finish.

You probably want something more like

while true; do
    sleep 3
    echo "Waiting ... $(date)"
done &
pid=$!
 
tail -f test.log | grep -iq --line-buffered 'Server has started succesfully'
kill "$pid"

Notice also the addition of the -q option for grep to avoid having it spill output to the controlling terminal, and the fixed syntax for printing the date. (Single quotes prevent any expansion, so use double quotes; and $date is not a defined variable, so I guess you meant to call the date command.) The --line-buffered option to grep may be necessary if the log file only grows slowly.

Demo: https://ideone.com/VK9dW0

Upvotes: 1

Saboteur
Saboteur

Reputation: 1428

tail -f test|while read; do
 (echo $REPLY|grep -i 'Server has started succesfully') && break
done

But you need some kind of additional condition for timeout

Upvotes: 0

Related Questions