42

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.

1
  • 2
    Indeed, as Andreas suggests above, you can use CTRL+\ to get the statistics without quitting ping.
    – George
    Dec 6, 2014 at 13:23

7 Answers 7

41

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
6
  • 41
    One slight addition: You can emit SIGQUIT from the terminal by hitting "Ctrl-\", no need to have a second terminal open and use kill. Jul 11, 2014 at 13:51
  • 9
    Also, on BSD variants (including OSX), ping summarizes with a SIGINFO signal, which can be triggered with Ctrl-T in the terminal. Mar 2, 2017 at 20:57
  • 4
    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. Aug 14, 2019 at 18:44
  • 2
    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. Aug 14, 2019 at 18:53
  • 3
    @StarsonHochschild, I would propose you to post your method as new answer. Apr 3, 2020 at 9:31
23

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
2
  • 2
    In a twist of fate, I actually contributed some code toward that particular feature after getting my answer from the selected best answer.
    – 2mac
    Dec 22, 2016 at 21:02
  • 2
    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
    – nealmcb
    Jun 4, 2018 at 19:00
10

On Mac it's Ctrl+T.

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

2
  • 1
    Kudos to @brian-clide, just noticed he answered before me in a comment. Jun 17, 2019 at 15:55
  • CTRL-4 works for me on mac
    – Alex R
    Aug 18 at 10:28
7

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
6

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
2

If your ping does not support a useful SIGQUIT (AIX, Solaris), here's one workaround -- an infinite ping loop where each ping only fires off (e.g.) 10 pings, so that you can see intermediate results.

while :; do ping -c 10 $HOST; done

To stop it, Control-C may only kill the ping command; you may need to suspend and then kill the job (Control-z; kill %).

This of course does not provide true running statistics -- just over the course of those 10 pings.

1

You can use watch utility

watch -n 3 ping -c 1 10.170.0.21

That will run the ping command every 3 seconds and show you the output continuously.

1
  • 3
    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.
    – 2mac
    Nov 14, 2017 at 12:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.