0

I have processes that after started, bind to an address and port. These processes are run in screen using exec so that the screen closes when the child process closes.

When killing the process, I use kill -9 PID, but sometimes the screen ends, yet when I restart the process, the old process is still using the port, and I have to terminate the process again.

I've also read that SIGKILL leaves sockets open, stale memory, random resources in use, so I turned to just plain kill PID, which is a SIGTERM.

Is a SIGTERM guaranteed to allow the process to unbind from the address and port, or is there a better alternative?

hexacyanide
  • 88,222
  • 31
  • 159
  • 162

1 Answers1

1

If you SIGKILL all the processes that keep a listening port open, it is guaranteed to close.

However, it might not close for a few minutes, while it's in the TIME_WAIT state, as required by the TCP specification (to let listening clients know the port is closed in case they miss the original closing packet).

Well behaved servers will open the socket with the option SO_REUSEADDR, allowing it to reclaim the same port on restart immediately, but this is application specific. Without it, the port will appear to be in use for a few minutes.

that other guy
  • 116,971
  • 11
  • 170
  • 194