Skip to main content

Reply to comment

Waiting until log message or timeout

Posted in

How do I make an init.d script wait for a startup/shutdown message to appear in a log file, but time out if it takes too long?

tail -f | grep -m 1 should do the trick. There are ways to make the tail -f time out. And (at least in my opinion) the tail should terminate with a broken pipe when the grep stops running, but it doesn't; the tail -f just keeps running.

Though it's likely I'm doing something wrong, I do have a workaround:

#!/bin/dash

(sleep 20 2>/dev/null)&
TIMER=$!

tail -n 0 -f log --pid $TIMER | \
    (R=`grep -cm 1 searchstring`; kill $TIMER 2>/dev/null; exit $R)

RESULT=$?

Reply

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <i> <strong> <b> <u> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <s> <strike> <pre>
  • Lines and paragraphs break automatically.

More information about formatting options