(How to Stop the Ping Command in Linux) Ping is a Linux-based networking utility program used to verify the connections between the devices. ping is used by default in Linux which creates a process that will continuously send packets to the specified address until interrupted. In this article, I will describe several methods to stop ping commands and will elaborate on when each of them is valid, as well as I will provide several more recommendations on what to do in Linux to control ping.
Stopping the ping Command
If you perform a ping command on any website on the Linux system, it will send packets and expect replies and it prints out the whole result on the terminal. I am going to describe several ways of killing ping when it’s continuously pinging in an endless loop.
1. With the Keyboard Interrupt scan way (Ctrl + C).
The simplest and probably the most frequent way to shut down the ping command is through the keyboard shortcut Ctrl + C the terminal where the ping command is running. This keyboard interrupt will stop the command from being processed at once.
Example
- Run the
ping
command as above. - To stop it, press
Ctrl + C
.
When you press Ctrl + C
, ping
will display a summary of the packets sent and received, including packet loss and the average time for a response
When to Use Ctrl + C
This is the simplest method and it is used if you have been conducting a ping directly with the terminal session, and you would like to stop it by yourself.
2. Specifying a Limit on the Number of Packets (-c
option)
Another way of writing ping is by using the -c option; this will put a stop to ping after a certain number of packets has been transmitted. In this way, ping will pause itself after it has sent out the exact number of packets required.
Example:
In this example:
- The
-c
option tellsping
to send only 4 packets. - After sending 4 packets,
ping
stops automatically and displays the statistics summary.
When to Use the -c
Option
This is very useful when you need to do ping tests or can ping and use it then you need to use the -c option. This is also useful when using scripts that call ping as it will have expected behavior each time.
3. Using timeout
to Limit the Runtime of ping
For those who want to run ping with a particular time rather than by the packet count, there is available the timeout command. This approach pauses ping after some time in seconds.
Example:
In this example:
- The
timeout
command limitsping
to 5 seconds. - After 5 seconds,
timeout
stopsping
automatically.
When to Use timeout
The timeout option is handy when you wish the ping operation to take place for a specific amount of time. It is useful in scripts or any automated service where ping should be completed after a certain time not depending on the actual count of packets.
4. Stopping ping
Using kill
Command
Ping can be stopped using Kill if it’s running in the background or in another terminal session. Once again, type the pin’s Process ID (PID) using the ps command and affectively terminate it using kill.
Step 1: Find the PID of ping
In this command:
ps aux
lists all running processes.grep ping
filters the output to show only lines that contain “ping.”
Look for the process ID (PID) of ping
in the output.
Step 2: Kill the Process
Once you have the PID, use kill
to stop the ping
process:
Replace [PID]
with the actual PID of the ping
command.
Example:
If ping
has a PID of 1234, you would run:
You can also use killall
to terminate all ping
processes at once:
When to Use kill
or killall
The kill
and killall
commands are useful when:
ping
is running in the background.- You need to stop
ping
from another terminal. Ctrl + C
ortimeout
isn’t accessible.
Additional Tips for Managing ping
in Linux
Here are some additional ping
options and methods that help in controlling ping
behavior.
Running ping
in the Background
If you want to run ping
in the background, add an ampersand &
at the end of the command:
The &
symbol puts ping
in the background and allows you to keep using the same terminal session.
Viewing Background Jobs and Stopping ping
To see a list of background jobs, use the jobs
command
To bring a background job to the foreground and then stop it, use fg
followed by Ctrl + C
:
Replace %1
with the job number.
Specifying Interval Between Pings with the -i
Option
The -i
option allows you to specify an interval between pings. This can reduce the number of packets sent and help control the ping
frequency.
Example:
In this example:
ping
will send packets every 5 seconds.
This helps monitor network stability over longer periods without overloading the network.
Quiet Mode with -q
Option
Using -q
will quiet the output of ping
, only showing a summary when it stops. This is useful if you want fewer details during the process.
Example:
In this example:
-q
quiets the output.-c 4
stopsping
after 4 packets.
Summary
The ping command is an invaluable tool for testing network connectivity, and Linux offers several ways to control and stop it:
Keyboard Interrupt (Ctrl + C): The simplest way to manually stop ping in the terminal.
Packet Count Limit (-c option): Shut down ping after sending a particular number of packets to be sent.
Timeout (timeout command): Capping of the ping to the given time in seconds.
Kill Command (kill or killall): Exits ping with process ID ideal for background applications.
Additional Options: The following options are available — -i to set an interval, -q for quiet mode, jobs for showing running processIDs for background processes, and fg to bring them to the foreground.
By familiarising yourself with the methods highlighted in this tutorial, you will be able to manage ping as a handy diagnosing tool in Linux-based networks.