英文:
How to list all processes/services running on different ports
问题
有没有一个命令可以列出所有运行在本地主机不同端口上的服务?
在我的情况下,当我在一个Angular应用上工作时,我可能会在localhost:4200上运行它,一个React应用在localhost:3000上运行,还有一个Redis服务器在localhost:6379上运行,等等。
有没有一种方法可以知道它们是否在运行,以及如何终止/停止它们?
英文:
Is there a command that lists all the services that are running on different ports of localhost?
In my case, when I'm working on an Angular app, I may run it on localhost:4200, a React app on localhost:3000 and a Redis server on localhost:6379, etc.
Is there a way of knowing if these are running and how can I kill/stop them?
答案1
得分: 9
你使用的操作系统是哪个?答案可能会根据操作系统的类型不同而有所不同,包括不同的发行版。
例如,在某些Linux发行版上,我更愿意使用ss -nltp
。
示例:
$ ss -nltp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 10 127.0.0.1:25 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 50 :::8080 :::*
LISTEN 0 128 :::22 :::*
解释:
-n, --numeric 不解析服务名称
(google.com --> 1.2.3.4)
-l, --listening 显示监听的套接字
(仅显示您正在监听的端口)
-p, --processes 显示使用套接字的进程
(包括锁定套接字的子进程)
-t, --tcp 仅显示TCP套接字
一个更一般的命令是netstat
。
示例:$ netstat -nl
请查看ss
的手册获取更多信息。
编辑:因为您说您正在使用Windows,您可以使用以下命令列出所有相关的进程(-n == 数字,-a == 所有,-o == 显示进程ID,-p TCP == 仅显示TCP):
netstat -nao -p TCP
最后一列将是进程ID,您可以使用taskkill
来终止进程:
taskkill /F /PID <PID>
其中/F
表示强制终止,/PID
指示下一个值是进程ID。
英文:
Which operating system are you using? The answer may differ depending on the type of the operating system, including different distributions.
For example, on some Linux distributions I'd rather use ss -nltp
.
Example:
$ ss -nltp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 10 127.0.0.1:25 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 50 :::8080 :::*
LISTEN 0 128 :::22 :::*
Explained:
-n, --numeric don't resolve service names
(google.com --> 1.2.3.4)
-l, --listening display listening sockets
(just the ports you're listening at)
-p, --processes show process using socket
(include sub processes locking sockets)
-t, --tcp display only TCP sockets
A more general command would be netstat
.
Example: $ netstat -nl
Please check the manual of ss
for more information.
Edit: Since you said you were using Windows, you can use this to list all the relevant processes (-n == numeric, -a == all, -o == show process id, -p TCP == show TCP only):
netstat -nao -p TCP
Last column would be the process ID, you can use taskkill
to kill the process:
taskkill /F /PID <PID>
Where /F
says forcefully kill and /PID
indicates the next value is the process ID.
答案2
得分: 1
在Windows上使用netstat -nba | FINDSTR "LISTEN"
来获取正在监听端口的进程(Pids)列表。
如果你需要查找特定端口,那么可以通过两次使用findstr来筛选:
netstat -nba | FINDSTR "LISTEN" | FINDSTR "3000"
在PowerShell中,你可以使用Stop-Process CMDlet和Id选项来停止进程:
Stop-Process -Id 1234
如果你想要在一个PowerShell命令中完成所有操作,可以使用以下方式之一:
Stop-Process -Id (Get-NetTCPConnection -LocalPort 3000).OwningProcess -Force
或者
Stop-Process -Id (Get-NetTCPConnection -LocalPort 6379).OwningProcess -Force
用于Redis。
英文:
On windows use netstat -nba | FINDSTR "LISTEN"
to get a list of processes (Pids) listening on a port
if you need to find a specific port, then pipe it through findstr twice
netstat -nba | FINDSTR "LISTEN" | FINDSTR "3000"
In powershell you can then use Stop-Process CMDlet with the Id option to stop the process
Stop-Process -Id 1234
if you want to do it all in one powershell command, you can go with
Stop-Process -Id (Get-NetTCPConnection -LocalPort 3000).OwningProcess -Force
or
Stop-Process -Id (Get-NetTCPConnection -LocalPort 6379).OwningProcess -Force
for redis
答案3
得分: 1
你可以使用批处理(cmd.exe)来执行这个任务
::列出进程
@Tasklist.exe
::列出服务
Sc.exe Query Type= service
::停止一个进程
Taskkill.exe /im "任务的图像名称.exe"
::停止一个服务
@%__APPDIR__%Net.exe Stop "服务的名称"
::启动一个服务
@%__APPDIR__%Net.exe Start "服务的名称"
要执行这些批处理文件:
- 打开记事本并复制其中一个代码以执行指定的任务。
- 将其保存为扩展名为 .bat 的文件。
- 打开 cmd.exe,拖放批处理文件并执行它。
英文:
You can use batch(cmd.exe) for this task
::List processes
@Tasklist.exe
::List services
Sc.exe Query Type= service
::Stop a process
Taskkill.exe /im "Image name of a task.exe"
::Stop a service
@%__APPDIR__%Net.exe Stop "Service name"
::Start a service
@%__APPDIR__%Net.exe Start "Service name"
To execute these bat files:
- Open notepad and copy one of the codes of them for the task specified.
- Save it as a file with .bat extension.
- Open cmd.exe and drag and drop the bat file and execute it.
答案4
得分: 0
尝试以下命令:
sudo netstat -ltnp
上述命令根据以下特性提供netstat信息:
- l:仅显示监听的套接字
- t:显示TCP连接
- n:以数字形式显示地址
- p:显示进程ID/程序名称
您的输出应该类似于:
协议 接收队列 发送队列 本地地址 外部地址 状态 PID/程序名称
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1784/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 9031/cupsd
tcp 0 0 0.0.0.0:15672 0.0.0.0:* LISTEN 1504/beam.smp
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 1798/postgres
tcp 0 0 0.0.0.0:7070 0.0.0.0:* LISTEN 1245/anydesk
tcp 0 0 0.0.0.0:70 0.0.0.0:* LISTEN 1803/nginx -g daemo
tcp 0 0 0.0.0.0:25672 0.0.0.0:* LISTEN 1504/beam.smp
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 1476/mongod
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 1739/mysqld
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 1683/redis-server 1
tcp 0 0 127.0.0.1:33485 0.0.0.0:* LISTEN 5582/chrome --type=
tcp 0 0 0.0.0.0:4369 0.0.0.0:* LISTEN 1736/epmd
tcp 0 0 127.0.0.1:5939 0.0.0.0:* LISTEN 2435/teamviewerd
tcp 0 0 127.0.0.1:21460 0.0.0.0:* LISTEN 15337/node
tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN 4671/dnsmasq
tcp6 0 0 :::22 :::* LISTEN 1784/sshd
tcp6 0 0 ::1:631 :::* LISTEN 9031/cupsd
tcp6 0 0 127.0.0.1:5563 :::* LISTEN 15337/node
tcp6 0 0 :::5672 :::* LISTEN 1504/beam.smp
tcp6 0 0 :::80 :::* LISTEN 2532/apache2
tcp6 0 0 :::4369 :::* LISTEN 1736/epmd
英文:
Try following command :
sudo netstat -ltnp
The above command gives netstat information based on the following features:
l: display only listening sockets
t: display tcp connection
n: display addresses in a numerical form
p: display process ID/ Program name
Your output should look something like :
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1784/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 9031/cupsd
tcp 0 0 0.0.0.0:15672 0.0.0.0:* LISTEN 1504/beam.smp
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 1798/postgres
tcp 0 0 0.0.0.0:7070 0.0.0.0:* LISTEN 1245/anydesk
tcp 0 0 0.0.0.0:70 0.0.0.0:* LISTEN 1803/nginx -g daemo
tcp 0 0 0.0.0.0:25672 0.0.0.0:* LISTEN 1504/beam.smp
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 1476/mongod
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 1739/mysqld
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 1683/redis-server 1
tcp 0 0 127.0.0.1:33485 0.0.0.0:* LISTEN 5582/chrome --type=
tcp 0 0 0.0.0.0:4369 0.0.0.0:* LISTEN 1736/epmd
tcp 0 0 127.0.0.1:5939 0.0.0.0:* LISTEN 2435/teamviewerd
tcp 0 0 127.0.0.1:21460 0.0.0.0:* LISTEN 15337/node
tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN 4671/dnsmasq
tcp6 0 0 :::22 :::* LISTEN 1784/sshd
tcp6 0 0 ::1:631 :::* LISTEN 9031/cupsd
tcp6 0 0 127.0.0.1:5563 :::* LISTEN 15337/node
tcp6 0 0 :::5672 :::* LISTEN 1504/beam.smp
tcp6 0 0 :::80 :::* LISTEN 2532/apache2
tcp6 0 0 :::4369 :::* LISTEN 1736/epmd
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论