YumYumYum YumYumYum - 6 months ago 33
Linux Question

Using BASH how can i kill TCP port 16969?

I have one application which use port TCP 16969. It requires sometimes a quick software kernel reboot on the fly. But if i launch it too fast, then i am locked with

Exception in thread "main" java.net.BindException: Address already in use

So without any excuse i want to trigger my BASH script which can kill any running or listening port with 16969. But how can i do that?

$ lsof -w -n -i tcp:16969 # this gives me a list of 50 lines but how can i tell apply kill to all on this port?


I think that:

lsof -i tcp:22 | grep LISTEN | awk '{print $2}' | xargs kill

Should do the trick.

To double check what commands it wants to run before letting it loose add an echo before the kill like this:

lsof -i tcp:22 | grep LISTEN | awk '{print $2}' | xargs echo kill

It'll then list the PIDs that it would ordinarily kill