Reputation: 40870
How do I manually find (and kill) process using the terminal that listen to/use my TCP ports? I'm on macOS.
Sometimes, after a crash or bug, my Rails app gets locked to port 3000, and I can't find it using ps -ef
...
When running
rails server
I get
Address already in use - bind(2) (Errno::EADDRINUSE)
The same issue happens when stopping the Node.js process. Even after the process is stopped and the app stops running, port 3000
is locked. When starting the app again, getting
Address already in use (Errno::EADDRINUSE)
Upvotes: 3121
Views: 4769749
Reputation: 15050
Quick and easiest solution:
kill -9 $(lsof -ti:3000)
For multiple ports:
kill -9 $(lsof -ti:3000,3001)
#3000 is the port to be freed
Kill multiple ports with single line command:
kill -9 $(lsof -ti:3000,3001)
#Here multiple ports 3000 and 3001 are the ports to be freed
lsof -ti:3000
If the port is occupied, the above command will return something like this: 82500 (Process ID)
lsof -ti:3001
82499
lsof -ti:3001,3000
82499 82500
kill -9 $(lsof -ti:3001,3000)
Terminates both 82499 and 82500 processes in accessing ports 3001 and 3000 in a single command.
For using this in package.json
scripts:
"scripts": {
"start": "kill -9 $(lsof -ti:3000,3001) && npm start"
}
In terminal you can use:
npm run start
Upvotes: 1229
Reputation: 342859
For macOS El Capitan and newer (or if your netstat doesn't support -p
), use lsof
:
lsof -i tcp:3000
Alternatively, you can use netstat
:
netstat -vanp tcp | grep 3000
Once you have the PID (Process ID) use:
kill -9 <PID>
Upvotes: 4922
Reputation: 15266
Add to ~/.bash_profile
or ~/.zshrc
:
function killTcpListen () {
kill -QUIT $(sudo lsof -sTCP:LISTEN -i tcp:$1 -t)
}
Then source ~/.bash_profile
or source ~/.zshrc
and run
killTcpListen 8080
Upvotes: 8
Reputation: 4716
To forcefully kill a process like that, use the following command
lsof -n -i4TCP:3000
OR lsof -i:3000
Where 3000 is the port number the process is running at
this returns the process id(PID) and run
kill -9 "PID"
Replace PID with the number you get after running the first command
What to do if you could not find PID of a process with lsof -i:PID
?
log in as a super user with sudo su
and run lsof -i:PID
again
Why kill -9 PID
does not work?
If you trying to kill a process with its PID and it still runs on another PID, it looks like you have started that process in a different account most probably root account. so Login in with sudo su
and execute kill -9 PID
Upvotes: 83
Reputation: 36240
Find:
sudo lsof -i :3000
Kill:
kill -9 <PID>
PLEASE NOTE: -9
kills the process immediately, and gives it no chance of cleaning up after itself. This may cause problems. Consider using -15
(TERM) or -3
(QUIT) for a softer termination which allows the process to clean up after itself.
Upvotes: 3367
Reputation:
After executing the kill commands, deleting the pid file might be necessary:
rm ~/mypath/myrailsapp/tmp/pids/server.pid
Upvotes: 1
Reputation: 59
just write on terminal
sudo kill -9 $(lsof -i :3000 -t)
hope , it's work.
Upvotes: 5
Reputation: 6517
kill -9 $(lsof -ti:3000)
works for me on macOS always.
If you're working on a node.js project, you can add it to package.json scripts like;
"scripts": {
...
"killme": "kill -9 $(lsof -ti:3000)",
...
},
then
npm run killme
--
Also if you want to add system wide alias for your macOS, follow these steps;
Navigate to your home directory:
cd ~
Open up .bash_profile or zsh profile using nano or vim:
vi .bash_profile
Add an alias (press i):
alias killme="kill -9 $(lsof -ti:3000)"
save file
restart terminal
type killme
to the terminal
Of course you can change port 3000 to what you want.
Upvotes: 10
Reputation: 8740
I have Macbook Pro (Catalina), 1 day I found the below issue while running the Django server (which runs on port 8000 by default):
python manage.py runserver 3000
(venv) Rishikeshs-MacBook-Pro:learn-django hygull$ python manage.py runserver 3000
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
July 27, 2021 - 23:54:05
Django version 3.2.3, using settings 'automated_classification.settings'
Starting development server at http://127.0.0.1:3000/
Quit the server with CONTROL-C.
Error: That port is already in use.
I tried to use the provided and executed but running only 1 of them was not solving my problem (I know there were some other answers too but somehow I solved my problem). E.g. I tried to rerun the above command but that too did not work (still the processes were active).
So I finally I used answer of @Cris with 1 more additional step as he & others have suggested. So my answer is just using their commands with Terminal output to make the executions more clearer to you.
lsof -P | grep ':3000' | awk '{print $2}'
(venv) Rishikeshs-MacBook-Pro:learn-django hygull$ lsof -P | grep ':3000' | awk '{print $2}'
36239
38272
Now I got the list of ids to kill, let's do.
kill -9 <PID>
(venv) Rishikeshs-MacBook-Pro:learn-django hygull$ kill -9 36239
(venv) Rishikeshs-MacBook-Pro:learn-django hygull$ kill -9 36239
-bash: kill: (36239) - No such process
(venv) Rishikeshs-MacBook-Pro:learn-django hygull$ kill -9 38272
(venv) Rishikeshs-MacBook-Pro:learn-django hygull$ kill -9 38272
-bash: kill: (38272) - No such process
And now, let's try to rerun the command.
python manage.py runserver 3000
(venv) Rishikeshs-MacBook-Pro:learn-django hygull$ python manage.py runserver 3000
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
July 27, 2021 - 23:55:53
Django version 3.2.3, using settings 'project.settings'
Starting development server at http://127.0.0.1:3000/
Quit the server with CONTROL-C.
kill -9 $(lsof -P | grep ':3000' | awk '{print $2}')
You can combine the above 2 steps in 1 line & execute to kill process listening on port 3000.
Upvotes: 1
Reputation: 1223
If you're using Zsh, and don't want to remember multi-pipe commands, just add next lines of code to ~/.zshrc
:
function murder() {
lsof -nti:$1 | xargs kill -9
}
And then any time you need to kill a process on a specific port, just use:
murder 3000
P.S feel free to rename the command and improve it :)
Upvotes: 1
Reputation: 991
To kill port 3000 on mac, run the below command
kill -9 $(lsof -t -i:3000 -sTCP:LISTEN)
Upvotes: 3
Reputation: 9263
This single command line is easy to remember:
npx kill-port 3000
You can also kill multiple ports at once:
npx kill-port 3000 3001 3002
For a more powerful tool with search:
npx fkill-cli
PS: They use third party javascript packages. npx
comes built in with Node.js.
Upvotes: 214
Reputation: 10622
Works for me for terminating node (Mac OS Catalina)
killall -9 node
Upvotes: 5
Reputation: 4973
To kill multi ports.
$ npx kill-port 3000 8080 8081
Process on port 3000 killed
Process on port 8080 killed
Process on port 8081 killed
Hope this help!
Upvotes: 63
Reputation: 95
I use this:
cat tmp/pids/server.pid | pbcopy
Then
kill -9 'paste'
Upvotes: 2
Reputation: 451
These two commands will help you find and kill server process
- lsof -wni tcp:3000
- kill -9 pid
Upvotes: 12
Reputation: 619
lsof -i tcp:port_number
- will list the process running on that port
kill -9 PID
- will kill the process
in your case, it will be
lsof -i tcp:3000
from your terminal
find the PID of process
kill -9 PID
Upvotes: 13
Reputation: 673
In mac OS
kill -9 $(lsof -i TCP:3000 | grep LISTEN | awk '{print $2}')
Upvotes: -4
Reputation: 3631
If you want a code free way - open activity manager and force kill node :)
Upvotes: 2
Reputation: 17851
To view the processes blocking the port:
netstat -vanp tcp | grep 3000
To Kill the processes blocking the port:
kill $(lsof -t -i :3000)
Upvotes: 33
Reputation: 9
Step 1: Find server which are running:
ps aux | grep puma
Step 2: Kill those server
Kill -9 [server number]
Upvotes: -2
Reputation: 20633
Here's a helper bash function to kill multiple processes by name or port
fkill() {
for i in $@;do export q=$i;if [[ $i == :* ]];then lsof -i$i|sed -n '1!p';
else ps aux|grep -i $i|grep -v grep;fi|awk '{print $2}'|\
xargs -I@ sh -c 'kill -9 @&&printf "X %s->%s\n" $q @';done
}
Usage:
$ fkill [process name] [process port]
Example:
$ fkill someapp :8080 node :3333 :9000
Upvotes: 3
Reputation: 465
Find and kill:
This single command line is easy and works correctly.
kill -9 $(lsof -ti tcp:3000)
Upvotes: 28
Reputation: 55
You should try this, This technique is OS Independent.
In side your application there is a folder called tmp, inside that there is an another folder called pids. That file contains the server pid file. Simply delete that file. port automatically kill itself.
I think this is the easy way.
Upvotes: 2
Reputation: 388
I made a little function for this, add it to your rc file (.bashrc
, .zshrc
or whatever)
function kill-by-port {
if [ "$1" != "" ]
then
kill -9 $(lsof -ni tcp:"$1" | awk 'FNR==2{print $2}')
else
echo "Missing argument! Usage: kill-by-port $PORT"
fi
}
then you can just type kill-by-port 3000
to kill your rails server (substituting 3000 for whatever port it's running on)
failing that, you could always just type kill -9 $(cat tmp/pids/server.pid)
from the rails root directory
Upvotes: 12
Reputation: 7946
TL;DR:
lsof -ti tcp:3000 -sTCP:LISTEN | xargs kill
If you're in a situation where there are both clients and servers using the port, e.g.:
$ lsof -i tcp:3000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 2043 benjiegillam 21u IPv4 0xb1b4330c68e5ad61 0t0 TCP localhost:3000->localhost:52557 (ESTABLISHED)
node 2043 benjiegillam 22u IPv4 0xb1b4330c8d393021 0t0 TCP localhost:3000->localhost:52344 (ESTABLISHED)
node 2043 benjiegillam 25u IPv4 0xb1b4330c8eaf16c1 0t0 TCP localhost:3000 (LISTEN)
Google 99004 benjiegillam 125u IPv4 0xb1b4330c8bb05021 0t0 TCP localhost:52557->localhost:3000 (ESTABLISHED)
Google 99004 benjiegillam 216u IPv4 0xb1b4330c8e5ea6c1 0t0 TCP localhost:52344->localhost:3000 (ESTABLISHED)
then you probably don't want to kill both.
In this situation you can use -sTCP:LISTEN
to only show the pid of processes that are listening. Combining this with the -t
terse format you can automatically kill the process:
lsof -ti tcp:3000 -sTCP:LISTEN | xargs kill
Upvotes: 4
Reputation: 2041
Using sindresorhus's fkill tool, you can do this:
$ fkill :3000
Upvotes: 6