1

How do I implement the "or every 60s" in the following example?

prints_output_in_random_intervals | \
while read line "or every 60s"
do
  do_something
done
3

Built-in bash documentation help read mentions:

-t timeout  time out and return failure if a complete line of input is
            not read withint TIMEOUT seconds.  The value of the TMOUT
            variable is the default timeout.  TIMEOUT may be a
            fractional number.  If TIMEOUT is 0, read returns success only
            if input is available on the specified file descriptor.  The
            exit status is greater than 128 if the timeout is exceeded

Since read will fail if it returns because the timeout is reached, such a condition will also cause your loop to exit. If you want to avoid this, you can ignore read's exit status like this:

while read -t 60 line || true; do
    ...
done

or

while true; do
    read -t 60 line
    ...
done
  • 1
    Or move the read inside the while loop and loop over some other variable or condition. – thrig Jul 19 '16 at 15:45
  • Actually, I don't want to break the output. I want Bash to call do_something whenever any of these conditions is meet. So when either prints_output_in_random_intervals produces some output or 60s have passed. – Lugaxx Jul 19 '16 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.