代码之家  ›  专栏  ›  技术社区  ›  oma

查找(并终止)进程锁定Mac上的端口3000

  •  1358
  • oma  · 技术社区  · 14 年前

    有时,在崩溃或一些bug之后,我的rails应用程序锁定了端口3000。我用ps-ef找不到它。。。

    做的时候

    rails server
    

    地址已在使用-绑定(2)(Errno::EADDRINUSE)

    rm ~/mypath/myrailsapp/tmp/pids/server.pid

    28 回复  |  直到 5 年前
        1
  •  3079
  •   Ronan Boiteau    6 年前
    1. 你可以试试 netstat

      netstat -vanp tcp | grep 3000
      
    2. 马科斯埃尔卡皮坦 -p ),使用 lsof

      sudo lsof -i tcp:3000 
      
    3. Centos 7公司 使用

      netstat -vanp --tcp | grep 3000
      
        2
  •  1906
  •   Zameer Ansari    5 年前

    查找:

    sudo lsof -i :3000
    

    kill -9 <PID>
    
        3
  •  211
  •   jamesmortensen Arjan    8 年前

    上面没有什么对我有用。其他有我经验的人可以尝试以下方法(为我工作):

    运行:

    lsof -i :3000 (where 3000 is your current port in use)
    

    ps ax | grep <PID>
    

    最后,“开始吧”:

    kill -QUIT <PID>
    
        4
  •  163
  •   Zlemini    8 年前

    lsof -ti:3000 | xargs kill
    

    t标志会从lsof输出中除去PID之外的所有内容,这样就可以很容易地终止它。

        5
  •  145
  •   Abhijith Sasikumar    4 年前

    :

    kill $(lsof -ti:3000)  #3000 is the port to be freed
    

    使用单行命令终止多个端口:

    kill $(lsof -ti:3000,3001)  #here multiple ports 3000 and 3001 are the ports to be freed
    

    lsof-ti:3001

    lsof-ti:30013000

    82500

    杀死$(lsof-ti:30013000)

    把这个用在 package.json

    "scripts": { "start": "kill $(lsof -ti:3000,3001) && npm start" }

        6
  •  115
  •   Bruno Lemos    5 年前

    这个命令行很容易记住:

    npx kill-port 3000

    对于更强大的搜索工具:

    npx fkill-cli


    PS:他们使用第三方javascript包。 npx

    资料来源: tweet | github

        7
  •  108
  •   alex    11 年前

    lsof -i:3000 .

    那就是“列出打开的文件”。这将为您提供进程列表以及它们使用的文件和端口。

        8
  •  59
  •   alex    11 年前

    在你的 .bash_profile terminate 3000过程:

    terminate(){
      lsof -P | grep ':3000' | awk '{print $2}' | xargs kill -9 
    }
    

    然后,打电话 $terminate 如果它被堵住了。

        9
  •  58
  •   Tadele Ayelegn    6 年前

    要强制终止这样的进程,请使用以下命令

    lsof -n -i4TCP:3000 
    

    其中3000是进程运行的端口号

    这将返回进程id(PID) 然后跑

    kill -9 "PID"
    

    用运行第一个命令后得到的数字替换PID

    For Instance, if I want kill the process running on port 8080

        10
  •  38
  •   Kris    12 年前
    lsof -P | grep ':3000' | awk '{print $2}'
    

    这将只给你pid,在MacOS上测试。

        11
  •  29
  •   JE42    8 年前

    在OS-X El Captain上的命令行中执行:

    kill -kill `lsof -t -i tcp:3000`
    

    lsof的简洁选项只返回PID。

        12
  •  28
  •   YBathia    7 年前

    杀死端口上进程的方法之一是使用python库:freeport( https://pypi.python.org/pypi/freeport/0.1.9

    # install freeport
    pip install freeport
    
    # Once freeport is installed, use it as follows
    $ freeport 3000
    Port 3000 is free. Process 16130 killed successfully
    
        13
  •  26
  •   Henry    6 年前

    netstat -vanp tcp | grep 3000

    要终止阻塞端口的进程:

    kill $(lsof -t -i :3000)

        14
  •  24
  •   Sourabh Bhagat    8 年前

    找到打开的连接

    lsof-i-P | grep-i“听”

    按进程ID终止

    杀死-9“PID”

        15
  •  20
  •   Dylan Breugne    6 年前

    找到并杀死:

    kill -9 $(lsof -ti tcp:3000)
    
        16
  •  14
  •   smooth Sourabh Bhagat    8 年前

    可能的实现方法:

    top命令是查看系统资源使用情况和查看占用最多系统资源的进程的传统方法。顶部显示进程列表,其中使用CPU最多的进程位于顶部。

    ps公司

    ps命令列出正在运行的进程。以下命令列出系统上运行的所有进程:

    ps -A
    

    您还可以通过grep管道输出,以搜索特定进程,而无需使用任何其他命令。以下命令将搜索Firefox进程:

    ps -A | grep firefox
    

    kill PID_of_target_process
    

    lsof公司

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

     lsof -i tcp:3000 
    
        17
  •  11
  •   Binh Ho    4 年前

    杀死多个端口。

    $ npx kill-port 3000 8080 8081
    
    Process on port 3000 killed
    Process on port 8080 killed
    Process on port 8081 killed
    

    希望你能帮上忙!

        18
  •  10
  •   Shan    5 年前

    lsof -i tcp:port_number -将列出在该端口上运行的进程

    kill -9 PID -会破坏进程

    对你来说,会的

    lsof -i tcp:3000 找到进程的PID

        19
  •  10
  •   Akj    5 年前

    这两个命令将帮助您找到并终止服务器进程

    1. lsof-西尼罗河tcp:3000个
        20
  •  5
  •   rofrol    6 年前

    添加到 ~/.bash_profile

    function killTcpListen () {
      kill -QUIT $(sudo lsof -sTCP:LISTEN -i tcp:$1 -t)
    }
    

    那么 source ~/.bash_profile 然后跑

    killTcpListen 8080

        21
  •  5
  •   Kodie Grantham    6 年前

    使用 sindresorhus fkill 工具,可以执行以下操作:

    $ fkill :3000
    
        22
  •  4
  •   Benjie    6 年前

    lsof -ti tcp:3000 -sTCP:LISTEN | xargs kill
    

    如果客户机和服务器都在使用端口,例如:

    $ 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)
    

    在这种情况下,您可以使用 -sTCP:LISTEN 只显示正在侦听的进程的pid。把这个和 -t

    lsof-titcp:3000-sTCP:听着|沙格斯杀了
    
        23
  •  4
  •   Caleb Keene    6 年前

    我做了一个小函数,把它添加到你的rc文件中( .bashrc , .zshrc

    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
    }
    

    那你就可以打字了 kill-by-port 3000 杀死你的rails服务器(用3000代替它运行的任何端口)

    kill -9 $(cat tmp/pids/server.pid)

        24
  •  1
  •   Arun P    6 年前

    你应该试试这个,这项技术与操作系统无关。

    在您的应用程序中有一个名为tmp的文件夹,其中还有一个名为pids的文件夹。 只需删除该文件。端口自动自杀。

    我认为这是最简单的方法。

        25
  •  1
  •   Miguel    6 年前

    这里有一个helper bash函数,可以通过名称或端口终止多个进程

    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
    }
    

    用法:

    $ fkill [process name] [process port]
    

    例子:

    $ fkill someapp :8080 node :3333 :9000
    
        26
  •  1
  •   Foram Thakral    5 年前

    你可以试试这个

    netstat -vanp tcp | grep 3000
    
        27
  •  0
  •   HannahCarney    6 年前

        28
  •  0
  •   Mihail hidr0 Kirilov    5 年前

    我用这个:

    cat tmp/pids/server.pid | pbcopy

    那么 kill -9 'paste'

        29
  •  -1
  •   Thiện Nguyễn    6 年前

    步骤1:查找正在运行的服务器: ps aux | grep puma 第二步:杀死那些服务器

        30
  •  -1
  •   NoughT    5 年前

    在mac OS中

    kill -9 $(lsof -i TCP:3000 | grep LISTEN | awk '{print $2}')