mles
mles

Reputation: 3456

Watch for a log line to appear with a timeout in a bash while loop

I do have a bash script to run a Jira migration. The Jira7 Docker Container should run until the Re-index is complete with a timeout of two hours, then start the Jira8 Container.

#!/bin/bash -ex

SECONDS=0

docker-compose -f /root/docker-compose-jira7.yml up -d

docker logs jira7 >& /tmp/jira7.log

while ! grep "Re-indexing is 100% complete" /tmp/jira7.log > /dev/null;  [[ $SECONDS -lt 7200 ]]
do
    sleep 60
    docker logs jira7 >& /tmp/jira7.log
done

docker-compose -f /root/docker-compose-jira7.yml down

docker-compose -f /root/docker-compose-jira8.yml up -d

printf \
  'It took %dh:%dm:%ds to run the migration process\n' \
  $(($SECONDS/3600)) $(($SECONDS%3600/60)) $(($SECONDS%60))

The line with the string I'm looking for in the log looks like this:

2020-07-24 16:07:56,347 SharedEntityIndexer:thread-6 INFO localadmin 951x74x1 1y619f7 XXX.XXX.XXX.XXX,172.19.0.2 /secure/admin/IndexReIndex.jspa [c.a.j.w.a.admin.index.IndexAdminImpl] Re-indexing is 100% complete. Current index: PortalPage

The while loop only looking for the log line to appear was working fine. Then I added the timeout option [[ $SECONDS -lt 7200 ]], but now the log line search is not working anymore.

What is the correct way to have these two conditions in a while loop?

Upvotes: 0

Views: 290

Answers (1)

chepner
chepner

Reputation: 530950

Use && so that both commands have to succeed to continue the loop. With ;, the exit status of ! grep ... is ignored and only the exit status of [[ ...]] is considered by the loop.

while ! grep "Re-indexing is 100% complete" /tmp/jira7.log > /dev/null &&
      [[ $SECONDS -lt 7200 ]]
do
    ...

Upvotes: 2

Related Questions