Check ping statistics without stopping

25,562

Solution 1

From the ping manpage (emphasis mine):

When the specified number of packets have been sent (and received) or if the program is terminated with a SIGINT, a brief summary is displayed. Shorter current statistics can be obtained without termination of process with signal SIGQUIT.

So this will work if you're fine with your stats being slightly less verbose:

# the second part is only for showing you the PID
ping 8.8.8.8 & jobs ; fg

<... in another terminal ...>

kill -SIGQUIT $PID

Short statistics look like this:

19/19 packets, 0% loss, min/avg/ewma/max = 0.068/0.073/0.074/0.088 ms

Solution 2

There is one more easy way to get the ping statistics durning it's execution: Just press Ctrl + | (vertical slash or it's also called pipe line)

I do personally use it very often, try it:

64 bytes from 192.168.1.1: icmp_seq=6 ttl=64 time=0.893 ms
64 bytes from 192.168.1.1: icmp_seq=23 ttl=64 time=0.862 ms
64 bytes from 192.168.1.1: icmp_seq=24 ttl=64 time=3.18 ms
64 bytes from 192.168.1.1: icmp_seq=35 ttl=64 time=0.877 ms
64 bytes from 192.168.1.1: icmp_seq=36 ttl=64 time=0.866 ms
**36/36 packets, 0% loss, min/avg/ewma/max = 0.832/0.993/0.930/3.185 ms**
64 bytes from 192.168.1.1: icmp_seq=37 ttl=64 time=0.909 ms
64 bytes from 192.168.1.1: icmp_seq=38 ttl=64 time=2.03 ms
64 bytes from 192.168.1.1: icmp_seq=39 ttl=64 time=0.839 ms
64 bytes from 192.168.1.1: icmp_seq=40 ttl=64 time=0.880 ms

Solution 3

On Mac it's Ctrl+T.

Ctrl+\ does the same as Ctrl+C, as it stops the ping after showing the stats.

Solution 4

Linux (Tested on Ubuntu 20.04)

Send the SIGQUIT signal.

Example output:

64 bytes from localhost (127.0.0.1): icmp_seq=138 ttl=64 time=0.021 ms
64 bytes from localhost (127.0.0.1): icmp_seq=139 ttl=64 time=0.022 ms
139/139 packets, 0% loss, min/avg/ewma/max = 0.014/0.022/0.022/0.057 ms
64 bytes from localhost (127.0.0.1): icmp_seq=140 ttl=64 time=0.090 ms
64 bytes from localhost (127.0.0.1): icmp_seq=141 ttl=64 time=0.025 ms

Send while running

CTRL+\ = quit according to stty -a.
These also work: CTRL+|; CTRL+4;

Send to one or more known pids

kill -SIGQUIT <pid> [...]

Send to all running

ps -o pid= -C ping | xargs -r kill -SIGQUIT

Periodically send to all running

while sleep 20; do ps -o pid= -C ping | xargs -r kill -SIGQUIT; done

As a background job

while sleep 20; do ps -o pid= -C ping | xargs -r kill -SIGQUIT; done &

FreeBSD (Tested on pfSense 2.4.5, based on FreeBSD 11.3)

Send the INFO signal.

Example output when sent via CTRL+T:

64 bytes from 127.0.0.1: icmp_seq=137 ttl=64 time=0.328 ms
64 bytes from 127.0.0.1: icmp_seq=138 ttl=64 time=0.028 ms
load: 0.18  cmd: ping 62483 [select] 144.69r 0.00u 0.01s 0% 2256k
139/139 packets received (100.0%) 0.018 min / 0.072 avg / 0.505 max
64 bytes from 127.0.0.1: icmp_seq=139 ttl=64 time=0.116 ms
64 bytes from 127.0.0.1: icmp_seq=140 ttl=64 time=0.027 ms

Example output when sent via kill:

64 bytes from 127.0.0.1: icmp_seq=137 ttl=64 time=0.328 ms
64 bytes from 127.0.0.1: icmp_seq=138 ttl=64 time=0.028 ms
139/139 packets received (100.0%) 0.018 min / 0.072 avg / 0.505 max
64 bytes from 127.0.0.1: icmp_seq=139 ttl=64 time=0.116 ms
64 bytes from 127.0.0.1: icmp_seq=140 ttl=64 time=0.027 ms

Send while running

CTRL+T = status according to stty -a.

Send to one or more known pids

kill -INFO <pid> [...]

Send to all running

ps -o comm= -o pid= | egrep '^ping[[:space:]]' | awk -F' ' '{print $2}' | xargs -r kill -INFO

Periodically send to all running

printf "while ({ sleep 20 })\nps -o comm= -o pid= | egrep '^ping[[:space:]]' | awk -F' ' '{print \0442}' | xargs -r kill -INFO\nend\n" | tcsh

As a background job

printf "while ({ sleep 20 })\nps -o comm= -o pid= | egrep '^ping[[:space:]]' | awk -F' ' '{print \0442}' | xargs -r kill -INFO\nend\n" | tcsh &

Notes and Thanks

Thank you @pmos for your answer which seeded my idea for the periodic method in the first place, and to all the other answers and comments which contributed to this one.

Thank you @VictorYarema for making the suggestion that I turn my comment into an actual answer. The essence of the method in the original comment is the same, but has evolved over time as follows:

  • Added the -r option to xargs to prevent it from running kill without a pid when there are no results from ps
  • To get rid of the header row in ps I used the -o pid= option to set the column header text to empty instead of h since h has different meanings for Linux and FreeBSD
    • While ps supports the explicit --no-headers option in Linux, the same is not true for FreeBSD
  • Moved the sleep command into the while test condition
  • Removed unnecessary quotes
  • Ported to FreeBSD

Solution 5

Try Ctrl+4

It shows a line like this:

312/312 packets, 0% loss, min/avg/ewma/max = 0.312/1.236/0.505/208.655 ms
Share:
25,562

Related videos on Youtube

2mac
Author by

2mac

Updated on September 18, 2022

Comments

  • 2mac
    2mac over 1 year

    Is there a way to tell ping to show its usual termination statistics without stopping the execution?

    For instance, I'd like to quickly view:

    --- 8.8.8.8 ping statistics ---
    2410 packets transmitted, 2274 received, +27 errors, 5% packet loss, time 2412839ms
    rtt min/avg/max/mdev = 26.103/48.917/639.493/52.093 ms, pipe 3
    

    without having to stop the program, thus losing the accumulated data.

  • Andreas Wiese
    Andreas Wiese almost 10 years
    One slight addition: You can emit SIGQUIT from the terminal by hitting "Ctrl-\", no need to have a second terminal open and use kill.
  • 2mac
    2mac over 7 years
    In a twist of fate, I actually contributed some code toward that particular feature after getting my answer from the selected best answer.
  • Brian Cline
    Brian Cline about 7 years
    Also, on BSD variants (including OSX), ping summarizes with a SIGINFO signal, which can be triggered with Ctrl-T in the terminal.
  • 2mac
    2mac over 6 years
    This doesn't actually answer the question. If you read carefully, the purpose was to have running stats. Running a new ping would lose the history and thus not give the desired information.
  • nealmcb
    nealmcb almost 6 years
    I'm surprised to see that, for me in Ubuntu Bionic running Terminal, according to xev, Ctrl + \ is the same as the key combination mentioned in other answers here: Ctrl + | and Ctrl + 4. All yield an XLookupString of 0x1c which is bound to SIGQUIT as shown by stty -a. See also keyboard shortcuts - Shell SIGKILL Keybinding - Super User
  • Stefan Rogin
    Stefan Rogin almost 5 years
    Kudos to @brian-clide, just noticed he answered before me in a comment.
  • Starson Hochschild
    Starson Hochschild almost 5 years
    I regularly have multiple terminals going with continuous pings going. I usually start while true; do ps -o "pid" -C "ping" h | xargs kill -SIGQUIT; sleep 20; done & in the first terminal before starting the ping. This allows me to start new pings and/or restart existing pings (to reset stats) and not need to track PIDs for kill.
  • Starson Hochschild
    Starson Hochschild almost 5 years
    Using this method means there is no need to use the & jobs ; fg part of the command in the answer since it's only purpose is reporting the PID... Sorry for the additional comment - can't edit my original comment anymore due to the 5 minute threshold.
  • Victor Yarema
    Victor Yarema about 4 years
    @StarsonHochschild, I would propose you to post your method as new answer.
  • Starson Hochschild
    Starson Hochschild about 4 years
    Thank you for the inspiration, @VictorYarema. I have submitted my method as an answer, ported it to FreeBSD, and tried to compile all of the info in the various other answers and comments into one place.