TLDR
lsof -i:<port> | awk 'NR==2{print $2}' | xargs kill
Replace <port>
with your port number.
Why do you need it?
Many things happen on the computer during coding. Your favourite IDE wants to restart and update, the system requires a security update, or you already run that process, but you cannot find it in the crowd on the screen.
The best idea is to restart the computer then, but there are cases you cannot afford to do that. You can then find and terminate that process.
How to do that?
You can use that command. The command should be available both on Linux and macOS:
lsof -i:<port> | awk 'NR==2{print $2}' | xargs kill
What does it:
- lsof is a list open files utility. Remember: in Linux (and macOS), everything is the file
-i:<port>
selects the files (open connections) that use your TCP/UDP portawk
- one of the many command line text editors available and preinstalledNR==2
- select the second line (the first is the header){print $2}
- prints second column - the process which uses your port.xargs
- helper utility. Allows to rewritekill $(lsof -i:3100 | awk 'NR==2{print $2}')
to the command abovekill
- terminates the program
Remember that the process which uses your port is usually not the same one launched by you (usually, this is a child process), so you will only make this port available.
Variations:
You can manually find that process using the command:
lsof -P -n -i:<port>
(-P
, -n
hides hostname and ports from the listing - they do not give practical information in this case)
And then terminate the process using
kill <process id (pid)>