This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
The community reviewed whether to reopen this question 7 months ago and left it closed:
Original close reason(s) were not resolved
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(30)
对于 macOS El Capitan 及更新版本(或者如果您的 netstat 不支持
-p
),请使用lsof
:或者,您可以使用
netstat
:一旦获得 PID(进程 ID),请使用:
For macOS El Capitan and newer (or if your netstat doesn't support
-p
), uselsof
:Alternatively, you can use
netstat
:Once you have the PID (Process ID) use:
Find:
Kill:
请注意:
-9
立即终止该进程,并且不给它自行清理的机会。这可能会导致问题。考虑使用-15
(TERM) 或-3
(QUIT) 进行软终止,允许进程自行清理。Find:
Kill:
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.快速且最简单的解决方案:
对于多个端口:
#3000 是要释放的端口
使用单行命令杀死多个端口:< /strong>
#这里多个端口3000和3001是要释放的端口
如果端口被占用,上面的命令将返回如下内容:82500(进程ID)
82499
82499
82500
在单个命令中终止 82499 和 82500 进程。
要在
package.json
脚本中使用它:在终端中您可以使用:
Quick and easiest solution:
For multiple ports:
#3000 is the port to be freed
Kill multiple ports with single line command:
#Here multiple ports 3000 and 3001 are the ports to be freed
If the port is occupied, the above command will return something like this: 82500 (Process ID)
82499
82499
82500
Terminates both 82499 and 82500 processes in a single command.
For using this in
package.json
scripts:In terminal you can use:
以上对我来说都不起作用。任何有我经验的人都可以尝试以下操作(对我有用):
运行:
然后检查报告的 PID 的状态:
最后,“开始吧”:
Nothing above worked for me. Anyone else with my experience could try the following (worked for me):
Run:
then check status of the reported PID :
finally, "begone with it":
行很容易记住:
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
使用端口 3000 提取进程 PID 并杀死它的单行代码。
-t 标志从 lsof 输出中删除除 PID 之外的所有内容,从而可以轻松杀死它。
A one-liner to extract the PID of the process using port 3000 and kill it.
The -t flag removes everything but the PID from the lsof output, making it easy to kill it.
您可以使用
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.
要强制终止这样的进程,请使用以下命令
,其中 3000 是进程正在运行的端口号
,返回进程 ID(PID)
并运行
将 PID 替换为运行第一个命令后获得的数字
如果使用
找不到进程的 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
Where 3000 is the port number the process is running at
this returns the process id(PID)
and run
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 runlsof -i:PID
againWhy
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 executekill -9 PID
在您的
.bash_profile
中,创建用于终止
3000 进程的快捷方式:然后,如果它被阻止,则调用
$terminate
。In your
.bash_profile
, create a shortcut forterminate
the 3000 process:Then, call
$terminate
if it's blocked.杀死多个端口。
希望这有帮助!
To kill multi ports.
Hope this help!
这只会给你 pid,在 MacOS 上测试过。
This will give you just the pid, tested on MacOS.
在 OS-X El Captain 上的命令行中执行:
lsof 的简洁选项仅返回 PID。
Execute in command line on OS-X El Captain:
Terse option of lsof returns just the PID.
终止端口上进程的方法之一是使用 python 库: freeport (https ://pypi.python.org/pypi/freeport/0.1.9)。安装后,只需:
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:
要查看阻止端口的进程:
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)
查找并杀死:
这个命令行很简单并且工作正常。
Find and kill:
This single command line is easy and works correctly.
找到打开的连接
通过进程 ID 杀死
Find the open connection
Kill by process ID
实现此目的的可能方法:
top
top 命令是查看系统资源使用情况并查看占用最多系统资源的进程的传统方法。顶部显示进程列表,其中使用最多 CPU 的进程位于顶部。
ps
ps 命令列出正在运行的进程。以下命令列出了系统上运行的所有进程:
您还可以通过 grep 管道输出来搜索特定进程,而无需使用任何其他命令。以下命令将搜索 Firefox 进程:
将信号传递给程序的最常见方法是使用 Kill 命令。
lsof
所有打开的文件以及打开它们的进程的列表。
或者
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:
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:
The most common way of passing signals to a program is with the kill command.
lsof
List of all open files and the processes that opened them.
or
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 portkill -9 PID
- will kill the processin your case, it will be
lsof -i tcp:3000
from your terminalfind the PID of process
kill -9 PID
我为此做了一个小函数,将其添加到您的 rc 文件(
.bashrc
、.zshrc
或其他文件)中,然后您只需输入
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)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这两个命令将帮助您找到并终止服务器进程
These two commands will help you find and kill server process
在 macOS 上一直对我有用。
如果您正在开发 Node.js 项目,则可以将其添加到 package.json 脚本中,例如;
然后
--
另外,如果您想为 macOS 添加系统范围的别名,请按照以下步骤操作;
导航到您的主目录:
使用 nano 或 vim 打开 .bash_profile 或 zsh 配置文件:
添加别名(按 i):
保存文件
重新启动 终端
类型
killme
到终端当然您可以将端口 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;
then
--
Also if you want to add system wide alias for your macOS, follow these steps;
Navigate to your home directory:
Open up .bash_profile or zsh profile using nano or vim:
Add an alias (press i):
save file
restart terminal
type
killme
to the terminalOf course you can change port 3000 to what you want.
添加到
~/.bash_profile
或~/.zshrc
:然后
source ~/.bash_profile
或source ~/.zshrc
代码>并运行killTcpListen 8080
Add to
~/.bash_profile
or~/.zshrc
:Then
source ~/.bash_profile
orsource ~/.zshrc
and runkillTcpListen 8080
使用 sindresorhus 的 fkill 工具,你可以这样做:
Using sindresorhus's fkill tool, you can do this:
适用于我终止节点(Mac OS Catalina)
Works for me for terminating node (Mac OS Catalina)
只要在终端上写下
希望,就可以了。
just write on terminal
hope , it's work.
TL;DR:
如果您处于客户端和服务器都使用该端口的情况,例如:
那么您可能不想同时杀死两者。
在这种情况下,您可以使用 -sTCP:LISTEN 来仅显示正在侦听的进程的 pid。将此与
-t
简洁格式相结合,您可以自动终止该进程:TL;DR:
If you're in a situation where there are both clients and servers using the port, e.g.:
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:我最喜欢的一句话:
sudo Kill `sudo lsof -t -i:3000`
my fav one-liner:
sudo kill `sudo lsof -t -i:3000`
这是一个帮助器 bash 函数,用于按名称或端口杀死多个进程
用法:
示例:
Here's a helper bash function to kill multiple processes by name or port
Usage:
Example:
你可以试试这个
You can try this
我使用:
lsof -wni tcp:3000
获取PID,以及:
kill -9
I use:
lsof -wni tcp:3000
Get the PID, and:
kill -9 <PID>