"tail -f" hangs with pipelines
Consider this toy script:
#!/sbin/sh echo "green" sleep 5 echo "green" sleep 5 echo "green" sleep 5 echo "purple" sleep 5 echo "green" sleep 5 echo "green"
Now run that in the background, sent to a file ("/tmp/foo"), and then run this pipeline:
tail -f /tmp/foo | grep purple | head -1
Ideally that will exit the second "purple" shows up, while the above script will continue to run for 10 more seconds.
Instead, the pipeline above hangs, and depending on the shell, either exits without output (sh), or hangs in this stack (bash, tcsh):
717: tail -f /tmp/foo feeff347 portfs (5, 4, 8047b28, 0, 0, 0) 08052706 follow (80656f0, 4, a, 0, 2, 0) + 364 08053b09 main (8047ccc, fef786e8, 8047d04, 8051a3f, 3, 8047d10) + 496 08051a3f _start (3, 8047de8, 8047ded, 8047df0, 0, 8047df9) + 83
Updated by Jon Strabala over 4 years ago
I don't think this is an issue at all with 'tail' rather it is the way multi-stage pipelines work (and in some cases interaction with a terminal) on most if not all *NIX systems - refer to:
You will see the same standard behavior if you 'pipe' your shell script as follows without using tail:
./toy_script.sh | grep purple | head -1