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?
CodePudding user response:
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
CodePudding user response:
The until
loop waits for grep
to finish.
You probably want something more like
tail -f /path/server.log | grep -iq 'Server has started succesfully' &
pid=$!
while true; do
sleep 3
kill -0 "$pid" 2>/dev/null && echo "Waiting ... $(date)" || break
done
echo "You are ready $(date)"
wait "$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.)