views:

132

answers:

4

Hello experts,

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

Sometiles, 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 immediatly and free the port ?

regards

+2  A: 

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.

Nikita Rybak
+2  A: 

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.

Justin Ethier
He can do this in Java by calling setReuseAddress http://download.oracle.com/javase/1.4.2/docs/api/java/net/ServerSocket.html
Peter G.
Looks like we were both thinking the same thing - see my edits :)
Justin Ethier
+3  A: 

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.

andcoz
A: 

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}')
sputnick
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
seems there is an error with this command: kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]