Mykhaylo Adamovych
Mykhaylo Adamovych

Reputation: 20966

How to wait for message to appear in log in shell

Could you please provide neat solution to block execution of the script until text snippet appear in the given file?

Upvotes: 2

Views: 4774

Answers (2)

Mykhaylo Adamovych
Mykhaylo Adamovych

Reputation: 20966

Wait forever

grep -q 'ProducerService started' <(tail -f logs/batch.log)

Wait with timeout

timeout 30s grep -q 'ProducerService started' <(tail -f logs/batch.log)

Wait with timeout, notify error

timeout 30s grep -q 'ProducerService started' <(tail -f logs/batch.log) || exit 1  

Wait for next inserted instance (by @Dan-Dev)

timeout 30s grep -q 'ProducerService started' <(tail -n0 -f logs/batch.log)

Upvotes: 14

skr
skr

Reputation: 2286

Use inotifywait
inotifywait efficiently waits for changes to files

example:-

  1. kill the process to be blocked
  2. inotifywait -q -e modify /path/to/file/containing/snippet
  3. check for the changes in the file
  4. if the change matches then restart the script

Upvotes: 1

Related Questions