In this article, we will delve into the process of killing a Process ID (PID) that is found via
lsof -i in a Bash script. This task is a common requirement when managing servers and network connections, especially when certain processes need to be terminated for troubleshooting or system maintenance.
To kill a PID found via
lsof -i in a Bash script, you can use the
kill command followed by the PID. However, it’s important to use
kill -15 (or
kill -SIGTERM) instead of
kill -9 as a best practice, to allow the process to terminate gracefully.
Introduction to lsof and PID
Before we proceed, let’s first understand what
lsof is a command-line utility in Unix and Unix-like systems that stands for “LiSt Open Files”. It provides information about files that are opened by processes.
A PID, or Process ID, is a unique number that identifies a process in the system. If a process is using a network port, you can find its PID using
Finding a PID with lsof -i
To find the PID of a process using a specific port, you can use the
lsof -i command followed by the protocol type (TCP or UDP) and the port number. For example:
lsof -i tcp:8080
This command will list all processes that are using TCP port 8080. The PID is located in the second column of the
Writing a Bash Script to Kill the PID
Now that we have the PID, we can write a Bash script to kill it. Here’s a basic script:
PORT=8080 # Replace with your desired port number
# Get the list of PIDs
PIDS=$(lsof -ti tcp:$PORT)
# Check if any PIDs are found
if [[ -z $PIDS ]]; then
echo "No processes found on port $PORT"
# Kill each PID
for PID in $PIDS; do
echo "Killing process $PID"
kill -9 $PID
Let’s break down what this script does:
#!/bin/bash: This line tells the system that this script should be run with Bash.
PORT=8080: This line sets the variable
PORTto 8080. Replace 8080 with the port number you’re interested in.
PIDS=$(lsof -ti tcp:$PORT): This line runs the
lsof -ti tcp:$PORTcommand and saves its output (the PIDs) in the
if [[ -z $PIDS ]]; then ... fi: This block checks if the
PIDSvariable is empty. If it is, the script prints a message and exits.
for PID in $PIDS; do ... done: This block iterates over each PID in the
PIDSvariable and kills it.
Important Note on Using kill -9
In the script above, we used
kill -9 to terminate the processes. The
-9 option sends the
SIGKILL signal, which forces the process to terminate immediately. While this is effective, it does not allow the process to perform any cleanup before it exits. Therefore, it should be used as a last resort. If possible, try using
kill -15 (or
kill -SIGTERM), which sends the
SIGTERM signal that requests the process to terminate gracefully.
In this article, we’ve learned how to find the PID of a process using a specific port with
lsof -i, and how to write a Bash script to kill that PID. Remember to use
kill -9 sparingly and try to allow processes to exit gracefully with
kill -15 when possible.
lsof command is used to list open files in Unix and Unix-like systems. It provides information about files that are opened by processes.
You can use the
lsof -i command followed by the protocol type (TCP or UDP) and the port number. For example,
lsof -i tcp:8080 will list all processes using TCP port 8080, with the PID located in the second column of the
PID stands for Process ID, which is a unique number that identifies a process in the system. It is used to manage and interact with processes.
To kill a process using its PID in a Bash script, you can use the
kill command followed by the PID. For example,
kill 1234 will send the default
SIGTERM signal to process ID 1234, requesting it to terminate gracefully.
kill -15 (or
kill -SIGTERM) sends the
SIGTERM signal to a process, requesting it to terminate gracefully. This allows the process to perform any necessary cleanup before exiting. On the other hand,
kill -9 sends the
SIGKILL signal, which forces the process to terminate immediately without any cleanup. It is recommended to use
kill -15 whenever possible.