This fuser 8080/tcp will print the PID of the process bound to that port.
And this fuser -k 8080/tcp will kill that process.
Works on Linux only. More universal is use of lsof -i4 (or 6 for IPv6).
General form:
# list the TCP process bound to port PORT
fuser PORT/tcp
# Example: list the TCP process bound to port 8080
fuser 8080/tcp
# list the UDP process bound to port PORT
fuser PORT/udp
# Example: list the UDP process bound to port 8080
fuser 8080/udp
Answer from nudzo on Stack OverflowThis fuser 8080/tcp will print the PID of the process bound to that port.
And this fuser -k 8080/tcp will kill that process.
Works on Linux only. More universal is use of lsof -i4 (or 6 for IPv6).
General form:
# list the TCP process bound to port PORT
fuser PORT/tcp
# Example: list the TCP process bound to port 8080
fuser 8080/tcp
# list the UDP process bound to port PORT
fuser PORT/udp
# Example: list the UDP process bound to port 8080
fuser 8080/udp
To list any process listening to the port 8080:
lsof -i:8080
To kill any process listening to the port 8080:
kill $(lsof -t -i:8080)
or more violently:
kill -9 $(lsof -t -i:8080)
(-9 corresponds to the SIGKILL - terminate immediately/hard kill signal: see List of Kill Signals and What is the purpose of the -9 option in the kill command?. If no signal is specified to kill, the TERM signal a.k.a. -15 or soft kill is sent, which sometimes isn't enough to kill a process.).
To install lsof, if needed:
sudo apt install lsof
How to kill a process on a port on Ubuntu - Stack Overflow
Killport - A Simple Script to Kill Processes on a Port
How to find and kill a Process Listening on a port in Linux? netstat and lsof command examples
ubuntu - How can I kill a process running on a specific IP and port? - Unix & Linux Stack Exchange
Videos
You could use lsof to find the process:
lsof -t -i:4444
would list only the pid of the process listening on port 4444. You could just say
kill `lsof -t -i:4444`
if you were brave.
You use lsof:
# lsof -n | grep TCP | grep LISTEN | grep 4444
The output will be something like:
pname 16125 user 28u IPv6 4835296 TCP *:4444 (LISTEN)
Where the first column is the process name, and the second column is the process id. You then parse the output, find out what the process id (PID) is and use kill command to kill it.
Have you ever encountered the issue of not being able to start a process because the port is already in use? Killport is a simple script that allows you to quickly kill any process running on a specified port.
Using Killport is easy. Simply provide the port number as a command-line argument and the script will automatically find and kill any process running on that port. The script works on both macOS and Linux, and it is easy to install.
killport
Give it a try and let me know what you think.
give a โจ star, if you liked it. Github
There are likely cleaner ways, but something along the lines of:
netstat -lnp | grep 'tcp .*127.0.0.1:9984' | sed -e 's/.*LISTEN *//' -e 's#/.*##' | xargs kill
Using ss we can get details of process/connections which are listening on a specific IP and port, for src 127.0.0.1 and port 80:
sudo ss -lp '( dport = :80 )' src 127.0.0.1
then we can only get the PID's using grep and kill them all using xargs and kill.
sudo ss -lp '( dport = :80 )' src 127.0.0.1 | grep -Po "(?<=pid=).*(?=,)"\
| sort | uniq | xargs kill
You can also use -a switch instead of -l to get a list of all listening and non-listening sockets.
You also can do by that way :
kill -9 $( lsof -i:?0003 -t )
kill $(netstat -ntpl 2>/dev/null | egrep "^tcp .*:[0-9]0003"|awk '{print $7}'|cut -d / -f 1 )
You may do kill -9 instead of you think the -9 is required. I redirect the netstat stderr because it omits a message when run as non-root which is unimportant for this purpose. I include tcp in the regex to filter out tcp6.