Linux:如何杀死使用端口1935的程序?

发布于 2024-09-27 21:50:48 字数 429 浏览 3 评论 0原文

我的 Linux 服务器上运行着 red5 服务器 (JAVA)。

有时,服务器会关闭。当我尝试重新启动它时,出现错误:

“绑定错误,此端口已在使用中”。

因此我尝试使用 killall -9 java 终止服务器 并尝试重新启动服务器:同样的错误。

我必须等待一段时间(大约 2-3 分钟)然后再次重新启动:这有效。

我只需要知道为什么当我终止该进程时,我仍然需要等待 2-3 分钟才能使端口 1935 空闲并且我可以再次运行服务器。

有没有办法立即终止该进程并释放端口?

I have a red5 server (JAVA) running on my Linux server.

Sometimes, the server shuts down. When I try to restart it I got an error:

"Binding error, this port is alerady in use".

So I try to kill the server with killall -9 java
and try to restart the server: same error.

I have to wait for a while (about 2-3 minutes) and restart it again: that works.

I just need to know why when I kill the process I still have to wait 2-3 minutes before port 1935 is free and I can run the server again.

Is there a way to kill this process immediately and free the port ?

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

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

发布评论

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

评论(6

薄荷梦 2024-10-04 21:50:49

如果您确定服务器的旧实例拥有该端口,只需运行 jps,在列表中找到您的服务器 pid,然后运行 ​​kill -9 my_pid

对于通用非 java进程,lsof -i :1935 通常对我有用。再次获取 pid 并终止该进程。

If you're sure old instance of your server holds the port, just run jps, find your server pid in the list and run kill -9 my_pid

For generic non-java process, lsof -i :1935 usually works for me. Again, take pid and kill this process.

得不到的就毁灭 2024-10-04 21:50:49

问题是kill 中的-9

如果使用 SIGKILL (-9) 终止进程,该进程将立即终止。因此,端口将保持分配状态,直到(几分钟后)操作系统注意到问题。在 SIGKILL 之前尝试 SIGHUP 和 SIGINT(按顺序)。

无论如何,请使用 netstat -a -t -p 验证哪个进程已获取端口。

The problem is the -9 in the kill.

If you kill a process using SIGKILL (-9), the process is terminated immediately. So the port remains allocated until (some minute later) the O.S. notices the problem. Try SIGHUP and SIGINT (in the order) before SIGKILL.

In any case, use netstat -a -t -p to verify which process has acquired the port.

凉世弥音 2024-10-04 21:50:49

立即进程终止并释放端口:

 fuser -k 1935/tcp

Immediately process termination and port release:

 fuser -k 1935/tcp
浸婚纱 2024-10-04 21:50:49

如果可能,您应该在程序设置其套接字时使用套接字SO_REUSEADDR 选项。这样,当程序重新启动时,您可以立即重新使用套接字,而不必等待 2-3 分钟。

请参阅 javadoc setReuseAddress 了解更多信息。尤其:

当 TCP 连接关闭时,连接可能会在连接关闭后的一段时间内保持超时状态(通常称为 TIME_WAIT 状态或 2MSL 等待状态)。对于使用众所周知的套接字地址或端口的应用程序,如果存在涉及套接字地址或端口的超时状态连接,则可能无法将套接字绑定到所需的 SocketAddress。

在使用bind(SocketAddress)绑定套接字之前启用SO_REUSEADDR,即使先前的连接处于超时状态,也可以绑定套接字。

If possible, you should use the socket SO_REUSEADDR option when your program sets up its socket. That way you can immediately reuse the socket when the program is restarted, instead of having to wait 2-3 minutes.

See the javadoc setReuseAddress for more information. In particular:

When a TCP connection is closed the connection may remain in a timeout state for a period of time after the connection is closed (typically known as the TIME_WAIT state or 2MSL wait state). For applications using a well known socket address or port it may not be possible to bind a socket to the required SocketAddress if there is a connection in the timeout state involving the socket address or port.

Enabling SO_REUSEADDR prior to binding the socket using bind(SocketAddress) allows the socket to be bound even though a previous connection is in a timeout state.

归属感 2024-10-04 21:50:49

这是一个方便的 oneliner:

kill $(fuser 1935/tcp)

This is a handy oneliner:

kill $(fuser 1935/tcp)
笑忘罢 2024-10-04 21:50:49

默认情况下不应使用kill -9。该进程无法清理内部的东西。
要终止使用端口 8000 的应用程序的 pid:

kill $(netstat -nptl | awk '/:8000/{gsub("/.*", ""); print $7}')

kill -9 should'nt be used by default. The process can't clean up internal things.
To kill the pid of the application using by exemple port 8000 :

kill $(netstat -nptl | awk '/:8000/{gsub("/.*", ""); print $7}')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文