None
None

Reputation: 2433

How to monitore the stdout of a command with a timer?

I'd like to know when an application hasn't print a line in stdout for N seconds.

Here is a reproducible example:

#!/bin/bash
dmesg -w | {
  while IFS= read -t 3 -r line
  do
    echo "$line"
  done
  echo "NO NEW LINE"
}
echo "END"

I can see the NO NEW LINE but the pipe doesn't stop and the bash doesn't continue. END is never displayed.

Source: https://unix.stackexchange.com/questions/117501/in-bash-script-how-to-capture-stdout-line-by-line

Upvotes: 0

Views: 84

Answers (1)

KamilCuk
KamilCuk

Reputation: 140960

How to exit from the brackets' code?

Not all commands exit when they can't write to output or receive SIGPIPE, and they will not exit until they actually notice they can't write to output. Instead, run the command in the background. If the intention is not to wait on the process, in bash you could just use process substitution:

{ 
     while IFS= read -t 3 -r line; do
        printf "%s\n" "$line"
    done
    echo "end"
} < <(dmesg -w)

You could also use coprocess. Or just run the command in the background with a pipe and kill it when done with it.

Upvotes: 1

Related Questions