Linux: How to kill programs that use port 1935?
Asked Answered
G

6

10

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 ?

Granvillegranvillebarker answered 16/10, 2010 at 12:50 Comment(1)
I do not believe the answers that blame SIGKILL for a failed cleanup of the port. The OS perfectly knows the process is gone and relinquishes its resources in the standard way. The standard way for a just closed TCP listen port is not to be availabe for some time to preclude connects to the wrong server. This can be avoided most easily by using SO_REUSEADDR as mentioned in Justin's answer.Arthromere
N
18

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.

Ned answered 16/10, 2010 at 12:55 Comment(0)
L
10

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.

Lum answered 16/10, 2010 at 13:4 Comment(0)
P
9

Immediately process termination and port release:

 fuser -k 1935/tcp
Plus answered 18/12, 2013 at 0:30 Comment(0)
A
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.

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.

Alric answered 16/10, 2010 at 12:56 Comment(2)
He can do this in Java by calling setReuseAddress download.oracle.com/javase/1.4.2/docs/api/java/net/…Arthromere
Looks like we were both thinking the same thing - see my edits :)Alric
S
2

This is a handy oneliner:

kill $(fuser 1935/tcp)
See answered 30/8, 2013 at 8:15 Comment(0)
Y
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 $(netstat -nptl | awk '/:8000/{gsub("/.*", ""); print $7}')
Yila answered 16/10, 2010 at 13:35 Comment(2)
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]Granvillegranvillebarker
seems there is an error with this command: kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]Granvillegranvillebarker

© 2022 - 2024 — McMap. All rights reserved.