Linux:如何杀死使用端口 1935 的程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3948960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linux: How to kill programs that use port 1935?
提问by yarek
I have a red5 server(JAVA) running on my Linux server.
我的 Linux 服务器上运行着一个red5 服务器(JAVA)。
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 javaand try to restart the server: same error.
所以我尝试用killall -9 java杀死服务器 并尝试重新启动服务器:同样的错误。
I have to wait for a while (about 2-3 minutes) and restart it again: that works.
我必须等待一段时间(大约 2-3 分钟)并再次重新启动它:有效。
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.
我只需要知道为什么当我终止进程时,我仍然需要等待 2-3 分钟才能使端口 1935 空闲,然后我才能再次运行服务器。
Is there a way to kill this process immediately and free the port ?
有没有办法立即终止这个进程并释放端口?
采纳答案by Nikita Rybak
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
如果您确定您的服务器的旧实例持有端口,只需运行jps
,在列表中找到您的服务器 pid 并运行kill -9 my_pid
For generic non-java process, lsof -i :1935
usually works for me. Again, take pid and kill this process.
对于通用的非 java 进程,lsof -i :1935
通常对我有用。再次,获取 pid 并终止此进程。
回答by Justin Ethier
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.
如果可能,您应该SO_REUSEADDR
在程序设置套接字时使用套接字选项。这样您就可以在程序重新启动时立即重用套接字,而不必等待 2-3 分钟。
See the javadoc setReuseAddressfor more information. In particular:
有关更多信息,请参阅 javadoc setReuseAddress。特别是:
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.
当 TCP 连接关闭时,连接可能会在连接关闭后的一段时间内保持超时状态(通常称为 TIME_WAIT 状态或 2MSL 等待状态)。对于使用众所周知的套接字地址或端口的应用程序,如果存在涉及套接字地址或端口的处于超时状态的连接,则可能无法将套接字绑定到所需的 SocketAddress。
在使用 bind(SocketAddress) 绑定套接字之前启用 SO_REUSEADDR 允许绑定套接字,即使之前的连接处于超时状态。
回答by andcoz
The problem is the -9
in the kill.
问题是-9
在杀死。
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.
如果您使用 SIGKILL (-9) 终止进程,该进程将立即终止。因此,该端口保持分配状态,直到(几分钟后)操作系统注意到该问题。在 SIGKILL 之前尝试 SIGHUP 和 SIGINT(按顺序)。
In any case, use netstat -a -t -p
to verify which process has acquired the port.
在任何情况下,用于netstat -a -t -p
验证哪个进程获取了端口。
回答by Gilles Quenot
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 -9。该过程无法清理内部事物。要使用示例端口 8000 终止应用程序的 pid:
kill $(netstat -nptl | awk '/:8000/{gsub("/.*", ""); print }')
回答by bartek
This is a handy oneliner:
这是一个方便的oneliner:
kill $(fuser 1935/tcp)
回答by Alexander Yancharuk
Immediately process termination and port release:
立即处理终止和端口释放:
fuser -k 1935/tcp