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

发布于 2024-09-25 16:12:48 字数 1817 浏览 7 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

终遇你 2024-10-02 16:12:48

对于 ma​​cOS El Capitan 及更新版本(或者如果您的 netstat 不支持 -p),请使用 lsof

lsof -i tcp:3000

或者,您可以使用 netstat

netstat -vanp tcp | grep 3000

一旦获得 PID(进程 ID),请使用:

kill -9 <PID>

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>
心意如水 2024-10-02 16:12:48

Find:

sudo lsof -i :3000

Kill:

kill -9 <PID>

请注意: -9 立即终止该进程,并且不给它自行清理的机会。这可能会导致问题。考虑使用 -15 (TERM) 或 -3 (QUIT) 进行软终止,允许进程自行清理。

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.

菊凝晚露 2024-10-02 16:12:48

快速且最简单的解决方案

kill -9 $(lsof -ti:3000)

对于多个端口:

kill -9 $(lsof -ti:3000,3001)

#3000 是要释放的端口

使用单行命令杀死多个端口:< /strong>

kill -9 $(lsof -ti:3000,3001)

#这里多个端口3000和3001是要释放的端口

lsof -ti:3000

如果端口被占用,上面的命令将返回如下内容:82500(进程ID)

lsof -ti:3001

82499

lsof -ti:3001,3000

82499
82500

kill -9 $(lsof -ti:3001,3000)

在单个命令中终止 82499 和 82500 进程。

要在 package.json 脚本中使用它:

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

在终端中您可以使用:

npm run start

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 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
遗心遗梦遗幸福 2024-10-02 16:12:48

以上对我来说都不起作用。任何有我经验的人都可以尝试以下操作(对我有用):

运行:

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

然后检查报告的 PID 的状态:

ps ax | grep <PID>

最后,“开始吧”:

kill -QUIT <PID>

Nothing above worked for me. Anyone else with my experience could try the following (worked for me):

Run:

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

then check status of the reported PID :

ps ax | grep <PID>

finally, "begone with it":

kill -QUIT <PID>
守护在此方 2024-10-02 16:12:48

行很容易记住:

npx Kill-port 3000

您还可以一次终止多个端口:

npx Kill-port 3000 3001 3002

这个命令 搜索:

npx fkill-cli


PS:他们使用第三方 javascript 包。 npx 内置于 Node.js。

来源:推文 | github

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.

Sources: tweet | github

音盲 2024-10-02 16:12:48

使用端口 3000 提取进程 PID 并杀死它的单行代码。

lsof -ti:3000 | xargs kill

-t 标志从 lsof 输出中删除除 PID 之外的所有内容,从而可以轻松杀死它。

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.

折戟 2024-10-02 16:12:48

您可以使用lsof -i:3000

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

You can use lsof -i:3000.

That is "List Open Files". This gives you a list of the processes and which files and ports they use.

情定在深秋 2024-10-02 16:12:48

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

lsof -n -i4TCP:3000  

OR lsof -i:3000

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

,返回进程 ID(PID)
并运行

kill -9 "PID"

将 PID 替换为运行第一个命令后获得的数字

例如,如果我想杀死在端口 8080 上运行的进程

如果使用 找不到进程的 PID,该怎么办lsof -i:PID?
使用 sudo su 以超级用户身份登录并再次运行 lsof -i:PID

为什么 kill -9 PID 不起作用?
如果您尝试使用 PID 杀死某个进程,但它仍然在另一个 PID 上运行,则看起来您已经在另一个帐户(很可能是 root 帐户)中启动了该进程。所以使用 sudo su 登录并执行 kill -9 PID

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

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

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

网白 2024-10-02 16:12:48

在您的 .bash_profile 中,创建用于终止 3000 进程的快捷方式:

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

然后,如果它被阻止,则调用 $terminate

In your .bash_profile, create a shortcut for terminate the 3000 process:

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

Then, call $terminate if it's blocked.

允世 2024-10-02 16:12:48

杀死多个端口。

$ npx kill-port 3000 8080 8081

Process on port 3000 killed
Process on port 8080 killed
Process on port 8081 killed

希望这有帮助!

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!

请别遗忘我 2024-10-02 16:12:48
lsof -P | grep ':3000' | awk '{print $2}'

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

lsof -P | grep ':3000' | awk '{print $2}'

This will give you just the pid, tested on MacOS.

你好,陌生人 2024-10-02 16:12:48

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

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

lsof 的简洁选项仅返回 PID。

Execute in command line on OS-X El Captain:

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

Terse option of lsof returns just the PID.

魂ガ小子 2024-10-02 16:12:48

终止端口上进程的方法之一是使用 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

One of the ways to kill a process on a port is to use the python library: freeport (https://pypi.python.org/pypi/freeport/0.1.9) . Once installed, simply:

# install freeport
pip install freeport

# Once freeport is installed, use it as follows
$ freeport 3000
Port 3000 is free. Process 16130 killed successfully
司马昭之心 2024-10-02 16:12:48

要查看阻止端口的进程:

netstat -vanp tcp | grep 3000

终止阻塞端口的进程:

kill $(lsof -t -i :3000)

To view the processes blocking the port:

netstat -vanp tcp | grep 3000

To Kill the processes blocking the port:

kill $(lsof -t -i :3000)

ま昔日黯然 2024-10-02 16:12:48

查找并杀死:

这个命令行很简单并且工作正常。

kill -9 $(lsof -ti tcp:3000)

Find and kill:

This single command line is easy and works correctly.

kill -9 $(lsof -ti tcp:3000)
绾颜 2024-10-02 16:12:48

找到打开的连接

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

通过进程 ID 杀死

kill -9 'PID'

Find the open connection

lsof -i -P | grep -i "listen"

Kill by process ID

kill -9 'PID'

独闯女儿国 2024-10-02 16:12:48

实现此目的的可能方法:

top

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

ps

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

ps -A

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

ps -A | grep firefox

将信号传递给程序的最常见方法是使用 Kill 命令。

kill PID_of_target_process

lsof

所有打开的文件以及打开它们的进程的列表。

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

或者

 lsof -i tcp:3000 

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 
风向决定发型 2024-10-02 16:12:48

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

kill -9 PID - 将终止您的情况下的进程

,它将是

lsof -我从你的终端发送 tcp:3000
查找进程的PID

kill -9 PID

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

大姐,你呐 2024-10-02 16:12:48

我为此做了一个小函数,将其添加到您的 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) Rails 根目录

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

若能看破又如何 2024-10-02 16:12:48

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

  1. lsof -wni tcp:3000
  2. kill -9 pid

These two commands will help you find and kill server process

  1. lsof -wni tcp:3000
  2. kill -9 pid
自找没趣 2024-10-02 16:12:48
kill -9 $(lsof -ti:3000)

在 macOS 上一直对我有用。

如果您正在开发 Node.js 项目,则可以将其添加到 package.json 脚本中,例如;

"scripts": {
    ...
    "killme": "kill -9 $(lsof -ti:3000)",
    ...
  },

然后

npm run killme

--

另外,如果您想为 macOS 添加系统范围的别名,请按照以下步骤操作;

导航到您的主目录:

cd ~

使用 nano 或 vim 打开 .bash_profile 或 zsh 配置文件:

vi .bash_profile

添加别名(按 i):

alias killme="kill -9 $(lsof -ti:3000)"

保存文件

重新启动 终端

类型 killme 到终端

当然您可以将端口 3000 更改为你想要什么。

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.

极致的悲 2024-10-02 16:12:48

添加到 ~/.bash_profile~/.zshrc

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

然后 source ~/.bash_profilesource ~/.zshrc代码>并运行

killTcpListen 8080

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

伴梦长久 2024-10-02 16:12:48

使用 sindresorhusfkill 工具,你可以这样做:

$ fkill :3000

Using sindresorhus's fkill tool, you can do this:

$ fkill :3000
命比纸薄 2024-10-02 16:12:48

适用于我终止节点(Mac OS Catalina)

killall -9 node

Works for me for terminating node (Mac OS Catalina)

killall -9 node
似狗非友 2024-10-02 16:12:48

只要在终端上写下

sudo kill -9 $(lsof -i :3000 -t)

希望,就可以了。

just write on terminal

sudo kill -9 $(lsof -i :3000 -t)

hope , it's work.

土豪 2024-10-02 16:12:48

TL;DR:

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 -ti tcp:3000 -sTCP:LISTEN | xargs kill

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
叹倦 2024-10-02 16:12:48

我最喜欢的一句话:
sudo Kill `sudo lsof -t -i:3000`

my fav one-liner:
sudo kill `sudo lsof -t -i:3000`

能否归途做我良人 2024-10-02 16:12:48

这是一个帮助器 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

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
小瓶盖 2024-10-02 16:12:48

你可以试试这个

netstat -vanp tcp | grep 3000

You can try this

netstat -vanp tcp | grep 3000
So尛奶瓶 2024-10-02 16:12:48

我使用:

lsof -wni tcp:3000

获取PID,以及:

kill -9

I use:

lsof -wni tcp:3000

Get the PID, and:

kill -9 <PID>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文