2010-10-16 154 views
8

我有一个red5 server(JAVA)在我的Linux服务器上运行。Linux:如何杀死使用1935端口的程序?

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

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

所以我尽量杀死killall -9 java的 服务器,并尝试重新启动服务器:同样的错误。

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

我只需要知道为什么当我杀死进程时,我仍然需要等待2-3分钟,然后我才能再次运行服务器。

有没有办法立即杀死这个过程并释放端口?

+1

我不相信将SIGKILL归咎于港口失败清理的答案。操作系统完全知道进程已经结束,并以标准方式放弃其资源。刚关闭的TCP监听端口的标准方式在一段时间内不能连接到错误的服务器。这可以通过使用Justin的答案中提到的SO_REUSEADDR来轻松避免。 – 2010-10-16 13:57:58

回答

16

如果你是你的服务器的肯定旧实例持有端口,只需运行jps,找到你的服务器的PID在列表中,然后运行kill -9 my_pid

对于一般非Java的过程中,通常lsof -i :1935对我的作品。再次,采取PID并杀死这个过程。

5

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

有关更多信息,请参阅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.

+0

他可以通过调用setReuseAddress http://download.oracle.com/javase/1.4.2/docs/api/java/net/ServerSocket.html – 2010-10-16 12:59:29

+0

来在Java中执行此操作看起来我们都在想同一件事 - 请参阅我的编辑:) – 2010-10-16 13:00:39

9

问题是在杀死。

如果使用SIGKILL(-9)终止进程,则会立即终止进程。所以港口仍然分配,直到(一分钟后)O.S.注意到问题。在SIGKILL之前尝试SIGHUP和SIGINT(按顺序)。

在任何情况下,请使用netstat -a -t -p来验证哪个进程已获取端口。

1

kill -9应该默认使用。该过程无法清理内部事物。 要使用为例8000端口杀应用程序的PID:

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

kill:usage:kill [-s sigspec | -n signum | -sigspec] pid | jobspec ...或kill -l [sigspec] – yarek 2010-10-17 14:58:36

+0

似乎这个命令有错误:kill:usage:kill [-s sigspec | -n signum | -sigspec] pid | jobspec ...或者kill -l [sigspec] – yarek 2010-10-17 14:59:07

1

这是一个方便的oneliner:

kill $(fuser 1935/tcp) 
7

立即终止进程和端口版本:

fuser -k 1935/tcp