0

I am trying to get ping to continuously write some times to a file. When I run

 $ ping 10 10.1.10.28 | perl -ne '/time=(\d+\.\d+)/ && print "$1\n"'

It returns one number every several milliseconds:

$ ping 10 10.1.10.28  | perl -ne '/time=(\d+\.\d+)/ && print "$1\n"'
191.523
312.225
127.506

However, when I redirect that to a file, and tail -f that file elsewhere, nothing happens. When I hit Ctrl-C, nothing is written to the file.

How can I force ping to write to STDOUT as new data comes in?

  • 1
    It's not ping, but perl that is not writing to std out. I believe perl waits until the incoming pipe is closed before running the command and writing the output. – jayhendren Dec 5 '14 at 23:06
1

You suffer from pipe buffering. Usually output to non interactive terminal is buffered to 4Kb blocks until delivered via pipe, so you have to disable it.

You could alter your command line like this:

$ stdbuf -oL ping 10.1.10.28  | perl -ne '$|=1; /time=(\d+\.\d+)/ && print "$1\n"' > file

stdbuf is part of coreutils.

$|=1; is the way to disable output buffering in perl.

P.D. I have removed the extra "10" in the ping command.

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.