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

时间:2022-09-14 18:12:22

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 java and 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 ?

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

6 个解决方案

#1


16  

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并杀死这个过程。

#2


9  

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)终止进程,则立即终止进程。所以端口保持分配直到(几分钟后)O.S.注意到这个问题。在SIGKILL之前尝试SIGHUP和SIGINT(按顺序)。

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

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

#3


7  

Immediately process termination and port release:

立即处理终止和端口释放:

 fuser -k 1935/tcp

#4


5  

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 setReuseAddress for 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.

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

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.

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

#5


1  

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默认情况下不应该使用。这个过程无法清理内部事物。使用exemple port 8000来杀死应用程序的pid:

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

#6


1  

This is a handy oneliner:

这是一个方便的oneliner:

kill $(fuser 1935/tcp)

#1


16  

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并杀死这个过程。

#2


9  

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)终止进程,则立即终止进程。所以端口保持分配直到(几分钟后)O.S.注意到这个问题。在SIGKILL之前尝试SIGHUP和SIGINT(按顺序)。

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

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

#3


7  

Immediately process termination and port release:

立即处理终止和端口释放:

 fuser -k 1935/tcp

#4


5  

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 setReuseAddress for 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.

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

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.

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

#5


1  

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默认情况下不应该使用。这个过程无法清理内部事物。使用exemple port 8000来杀死应用程序的pid:

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

#6


1  

This is a handy oneliner:

这是一个方便的oneliner:

kill $(fuser 1935/tcp)