Linux (redhat,centos, and MACOS) Releases occupied ports
Note: You must use the root user to perform this operation; otherwise, you cannot see the port process
My usage scenario is:
When the container is started, my service automatically starts the socket port 9001,9002. When I close the container, ports 9001 and 9002 are not released in time due to code problems. As a result, when I start the container for the second time, the message indicating that the port is occupied is kept
1. Run netstat -tln to check whether all ports are occupied. You can see that ports 9001 and 9002 are occupied, so I need to release these two ports
2. Enter lsof -I :9001 and add a colon (:) to view the process Id of the occupied port, as shown in the following figure
3. Now that you know the process id, kill the current process by killing -9 3340 press Enter
4. Now that we have killed it, we need to confirm that it is dead. We will type netstat -tln See,9001 and 9002 are gone, because 9001 and 9002 are two socket ports that depend on each other. If you kill one of them, the other one terminates, so they both disappear
For more information, see ibo-linux (Redhat,CentOS) releasing occupied ports