Question

Why read -t doesn't time out when reading from pipe on RHEL5 or RHEL6?

Here is my example which doesn't timeout on my RHEL boxes wile reading from the pipe:

tail -f logfile.log | grep 'something' | read -t 3 variable

If I'm correct read -t 3 should timeout after 3 seconds?

Many thanks in advance.

Chris

GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
Was it helpful?

Solution 2

While not a direct answer to your specific question, you will need to run something like

read -t 3 variable < <( tail -f logfile.log | grep "something" )

in order for the newly set value of variable to be visible after the pipeline completes. See if this times out as expected.


Since you are simply using read as a way of exiting the pipeline after a fixed amount of time, you don't have to worry about the scope of variable. However, grep may find a match without printing it within your timeout due to its own internal buffering. You can disable that (with GNU grep, at least), using the --line-buffered option:

tail -f logfile.log | grep --line-buffered "something" | read -t 3

Another option, if available, is the timeout command as a replacement for the read:

timeout 3 tail -f logfile.log | grep -q --line-buffered "something"

Here, we kill tail after 3 seconds, and use the exit status of grep in the usual way.

OTHER TIPS

The solution given by chepner should work.

An explanation why your version doesn't is simple: When you construct a pipe like yours, the data flows through the pipe from the left to the right. When your read times out however, the programs on the left side will keep running until they notice that the pipe is broken, and that happens only when they try to write to the pipe.

A simple example is this:

cat | sleep 5

After five seconds the pipe will be broken because sleep will exit, but cat will nevertheless keep running until you press return.

In your case that means, until grep produces a result, your command will keep running despite the timeout.

I dont have a RHEL server to test your script right now but I could bet than read is exiting on timeout and working as it should. Try run:

grep 'something' | strace bash -c "read -t 3 variable"

and you can confirm that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top