There are three options:
Manually interrupt the
ping command using Ctrl+C, as described in @steeldriver's answer.
ping's "count" option (
-c COUNT) to send exactly COUNT pings and then terminate automatically, as described in @bodhi.zazen's answer.
ping's "deadline" option (
-w DEADLINE) to run for exactly DEADLINE seconds and then terminate automatically.
Of course you can combine the three options. If you pass both a "count" and a "deadline" option, ping will terminate as soon as the first event occurs. And Ctrl+C works always anyway.
Here's an example with a "count" of maximal 10 pings and a "deadline" of 5 seconds, but which got terminated manually using Ctrl+C after 3 pings (
$ ping -c 10 -w 5 askubuntu.com
PING askubuntu.com (184.108.40.206) 56(84) bytes of data.
64 bytes from 220.127.116.11: icmp_seq=1 ttl=56 time=54.6 ms
64 bytes from 18.104.22.168: icmp_seq=2 ttl=56 time=52.5 ms
64 bytes from 22.214.171.124: icmp_seq=3 ttl=56 time=54.0 ms
--- askubuntu.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 52.582/53.733/54.611/0.850 ms