How to Kill a Process That Opens a TCP/UDP Port?


Killing a process that opens a TCP port can be necessary in various situations, such as when you want to stop a misbehaving or unwanted application or service. Here are the general steps to kill a process that has opened a specific TCP port:

How to Identify the Process That Opens a TCP/UDP Port?

First, you need to identify the process that is using the TCP port you want to close. You can use several methods for this:

Using netstat

Open a terminal or command prompt and run the following command to list all active network connections and associated processes:

1
netstat -ano | grep :port_number
netstat -ano | grep :port_number

Replace port_number with the actual port number you want to investigate. Note down the Process ID (PID) associated with the port.

For example:

1
netstat -ano | grep :8080
netstat -ano | grep :8080

Using lsof (Linux/Unix/macOS)

On Unix-based systems, you can use the lsof command to list open files (including network connections):

1
lsof -i :<port_number>
lsof -i :<port_number>

Again, note down the PID of the process.

Terminate the Process

Once you have identified the PID of the process using the port, you can terminate it using the appropriate command:

Kill a Process On Windows

Use the taskkill command with the /F (force) flag and the PID:

1
taskkill /F /PID PID
taskkill /F /PID PID

Kill a Process On Linux/Unix/macOS

Use the kill command with the PID:

1
kill PID # you may sudo this
kill PID # you may sudo this

Alternatively, if the process doesn’t respond to a regular kill signal, you can use kill -9 to forcefully terminate it:

1
kill -9 PID # you may sudo this
kill -9 PID # you may sudo this

Verify Termination:
After killing the process, you can recheck if the TCP port is now closed by running the netstat or lsof command again. The port should no longer be listed.

Please exercise caution when forcefully terminating processes, as it can lead to data loss or instability in some cases. It’s recommended to only terminate processes that you are sure are causing problems and are safe to stop.

Bash Script to Kill Any Processes that Open a Port (TCP/UDP)

We can wrap above to a bash script so that we can kill any hanging processes that occupy ports. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash
 
if [[ $# -eq 0 ]] ; then
    echo 'Usage: ./$0 <port> <port> <port> ...'
    exit 0
fi
 
cnt=0
for port in $@; do
    while :; do
        processid=$(sudo lsof -i :$port | tail -1 | awk '{print $2}')
        if [[ ! -z "$processid" ]]; then
            echo "Killing process $processid"
            sudo kill -9 $processid
            cnt=$((cnt+1))
        else
            break
        fi
    done
done
 
exit $cnt
#!/bin/bash

if [[ $# -eq 0 ]] ; then
    echo 'Usage: ./$0 <port> <port> <port> ...'
    exit 0
fi

cnt=0
for port in $@; do
    while :; do
        processid=$(sudo lsof -i :$port | tail -1 | awk '{print $2}')
        if [[ ! -z "$processid" ]]; then
            echo "Killing process $processid"
            sudo kill -9 $processid
            cnt=$((cnt+1))
        else
            break
        fi
    done
done

exit $cnt

Example usage:

1
./kill-processes-that-open-the-ports.sh 8080 8081 8082
./kill-processes-that-open-the-ports.sh 8080 8081 8082

You can pass multiple ports to the bash script, as shown above. And the return value (exit code) is the number of the processes that are killed. We pass “sudo” here to grep all the processes including those that are not visible to the current user – but you may remove it as required.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
675 words
Last Post: Bash Scripts to Check if a Domain or URL is Ping-able or Accessible
Next Post: How to Extract a Domain Name from a Full URL in BASH?

The Permanent URL is: How to Kill a Process That Opens a TCP/UDP Port?

Leave a Reply