我尝试在tomcat /bin目录下使用。/shutdown.sh关闭tomcat。但发现服务器没有正确关闭。因此我无法重新启动我的tomcat运行在端口8080上。

我想终止在8080上运行的tomcat进程。我首先想要在特定端口(8080)上运行的进程列表,以便选择要终止哪个进程。


当前回答

sudo apt-get install psmisc (or sudo yum install psmisc)
sudo fuser 80/tcp

结果: 80/tcp: 1858 1867 1868 1869 1871

逐个杀死进程

杀死-9 1858

其他回答

我正在Yocto Linux系统上工作,该系统具有有限的可用Linux工具集。我想终止使用特定端口的进程(1883年)。

首先,我使用以下命令查看我们正在侦听的端口:

root@root:~# netstat -lt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
tcp        0      0 0.0.0.0:hostmon         0.0.0.0:*               LISTEN      
tcp        0      0 localhost.localdomain:domain 0.0.0.0:*               LISTEN      
tcp        0      0 0.0.0.0:9080            0.0.0.0:*               LISTEN      
tcp        0      0 0.0.0.0:1883            0.0.0.0:*               LISTEN      
tcp        0      0 :::hostmon              :::*                    LISTEN      
tcp        0      0 localhost:domain        :::*                    LISTEN      
tcp        0      0 :::ssh                  :::*                    LISTEN      
tcp        0      0 :::1883                 :::*                    LISTEN      

接下来,我找到了使用端口1883的进程的名称,方法如下:

root@root:~# fuser 1883/tcp
290 
root@root:~# ps | grep 290
  290 mosquitt 25508 S    /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
12141 root      8444 S    grep 290

正如我们在上面看到的,使用端口1883的是程序/usr/sbin/ mosquito。

最后,我终止了这个过程:

root@root:~# systemctl stop mosquitto

我使用systemctl,因为在这种情况下,它是一个systemd服务。

输出所有在<port_number>上运行的进程id:

fuser -n tcp <port_number> 

它还输出一些东西到stderr,所以:

fuser -n tcp <port_number> 2> /dev/null

然后我们可以将这些进程id提供给kill命令:

sudo kill $(fuser -n tcp <port_number> 2> /dev/null)

如果你经常这样做,你也可以把它放在一个函数中:

function killport() {
    sudo kill $(fuser -n tcp $1 2> /dev/null)
}

这是Windows的解决方案:

C:\Users\Niroshan>netstat -ano|findstr "PID :8080"

Proto Local Address Foreign Address State PID
TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 18264

taskkill /pid 18264 /f

选项1在特定端口上只杀死LISTEN的一行程序:

kill -9 $(lsof -t -i:3000 -sTCP:LISTEN)

选项2如果你安装了npm,你也可以运行

npx kill-port 3000

这将杀死端口80上运行的程序

sudo fuser -k 80/tcp