Find (and kill) process locking port 3000 on Mac

2019-01-02 16:10发布

How do I find processes that listens to/uses my tcp ports? I'm on mac os x.

Sometimes, after a crash or some bug, my rails app is locking port 3000. I can't find it using ps -ef... How do I find the stupid thing and kill it, brutally... ?

When doing

rails server

I get

Address already in use - bind(2) (Errno::EADDRINUSE)

2014 update:

To complete some of the answers below: After executing the kill commands, deleting the pid file might be necessary rm ~/mypath/myrailsapp/tmp/pids/server.pid

标签: macos process
24条回答
皆成旧梦
2楼-- · 2019-01-02 16:46

A one-liner to extract the PID of the process using port 3000 and kill it.

lsof -ti:3000 | xargs kill

The -t flag removes everything but the PID from the lsof output, making it easy to kill it.

查看更多
春风洒进眼中
3楼-- · 2019-01-02 16:47

Possible ways to achieve this:

top

The top command is the traditional way to view your system’s resource usage and see the processes that are taking up the most system resources. Top displays a list of processes, with the ones using the most CPU at the top.

ps

The ps command lists running processes. The following command lists all processes running on your system:

ps -A

You could also pipe the output through grep to search for a specific process without using any other commands. The following command would search for the Firefox process:

ps -A | grep firefox

The most common way of passing signals to a program is with the kill command.

kill PID_of_target_process

lsof

List of all open files and the processes that opened them.

lsof -i -P | grep -i "listen"
kill -9 PID

or

 lsof -i tcp:3000 
查看更多
墨雨无痕
4楼-- · 2019-01-02 16:47

Find and kill:

This single command line is easy and works correctly.

kill -9 $(lsof -ti tcp:3000)
查看更多
人气声优
5楼-- · 2019-01-02 16:48

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.

查看更多
只若初见
6楼-- · 2019-01-02 16:49

You should try this code using the terminal:

$ killall -9 ruby
查看更多
泛滥B
7楼-- · 2019-01-02 16:52
  1. You can try netstat

    netstat -vanp tcp | grep 3000
    
  2. For macOS El Capitan and newer (or if your netstat doesn't support -p), use lsof

    sudo lsof -i tcp:3000 
    
  3. For Centos 7 use

    netstat -vanp --tcp | grep 3000
    
查看更多
登录 后发表回答