Linux:如何杀死使用端口1935的程序?
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您确定服务器的旧实例拥有该端口,只需运行
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 runkill -9 my_pid
For generic non-java process,
lsof -i :1935
usually works for me. Again, take pid and kill this process.问题是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.立即进程终止并释放端口:
Immediately process termination and port release:
如果可能,您应该在程序设置其套接字时使用套接字
SO_REUSEADDR
选项。这样,当程序重新启动时,您可以立即重新使用套接字,而不必等待 2-3 分钟。请参阅 javadoc setReuseAddress 了解更多信息。尤其:
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:
这是一个方便的 oneliner:
This is a handy oneliner:
默认情况下不应使用kill -9。该进程无法清理内部的东西。
要终止使用端口 8000 的应用程序的 pid:
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 :