Question: When I execute ping command from the command line, it keeps sending the packets until I hit CTRL-C to terminate the ping command output. How can I execute ping command only for N number of packets and terminate the output automatically?
Answer: Use ping option -c to specify the number of packets. After sending N number of packets, ping command will terminate automatically as explained below.
Ping Command – Interactive Mode
In the following example, you have to press CTRL-C to terminate the ping command output.
$ ping 0 PING 0 (127.0.0.1) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.023 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.006 ms 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.004 ms 64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.005 ms .... Note: Press CTRL-C to terminate. --- 0 ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 3999ms rtt min/avg/max/mdev = 0.004/0.008/0.023/0.007 ms
- Note: ping 0 — pings the local host.
Ping Command – Non Interactive Mode (Specify number of packets to be sent)
In the following example, ping command will send only 2 packets and you don’t need to press CTRL-C to terminate the output.
$ ping 0 -c 2 PING 0 (127.0.0.1) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.024 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.006 ms --- 0 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 999ms rtt min/avg/max/mdev = 0.006/0.015/0.024/0.009 ms
Ping Command Option -c Usages
Redirect the ping command output to a file
$ ping 127.0.0.1 -c 2 > ping-output.txt $ cat ping-output.txt PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.015 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.005 ms --- 127.0.0.1 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 999ms rtt min/avg/max/mdev = 0.005/0.010/0.015/0.005 ms
Redirect to another process. This example shows only the ping output summary (last two lines).
$ ping 127.0.0.1 -c 10 | tail -2 10 packets transmitted, 10 received, 0% packet loss, time 8999ms rtt min/avg/max/mdev = 0.005/0.006/0.014/0.003 ms
Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.Get the Linux Sysadmin Course Now!
If you enjoyed this article, you might also like..
|
|
|
|






My name is Ramesh Natarajan. I will be posting instruction guides, how-to, troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to write articles that will either teach you or help you resolve a problem. Read more about
{ 1 comment… read it below or add one }
This doesn’t work in Windows 7. The command is not -c, but -n. Format:
ping WIP -n #
Where [WIP] is the website or IP address to be pinged, and # is the number of packets to send.