How can I release a port that is still in use but not bound to a process?

5,255

This did the trick

sudo kill -9 `ps -C python | grep python | grep -v grep | awk '{print $1}'`
Share:
5,255

Related videos on Youtube

Trindaz
Author by

Trindaz

Updated on September 18, 2022

Comments

  • Trindaz
    Trindaz over 1 year

    I'm running a Python application that spawns multiple processes. It uses port 56008 to listen for client requests. Sometimes after closing it the port it used is still "in use". There are still some python processes that appear to be running but trying to kill them using kill doesn't seem to have any affect.

    How can I free it up manually so I can restart my application?

    More info:

    Output of ps -C python

      PID TTY          TIME CMD
    30118 pts/1    00:00:00 python
    30131 pts/1    00:00:00 python
    

    *There are about 30 lines in total of the format [pid] pts/1 00:00:00 python

    Output of netstat -tulp (relevant line only):

    Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
    tcp        0      0 *:56008                 *:*                     LISTEN      -
    
    • David Schwartz
      David Schwartz almost 11 years
      There are no other lines that refer to port 56008? Are you 100% sure?
  • Falcon Momot
    Falcon Momot almost 11 years
    You could probably simplify that substantially to killall -9 python, unless things which called python because python was in their arguments are also stuck. It's interesting, nonetheless, that netstat didn't report python as owning the socket.
  • Trindaz
    Trindaz almost 11 years
    @Falcon Very - I think it's related to them being sub-processes, and the fact that the parent process had died.
  • Flup
    Flup almost 11 years
    Maybe netstat wasn't run as root or the owner of the processes.
  • Falcon Momot
    Falcon Momot almost 11 years
    That seems more likely.
  • Trindaz
    Trindaz almost 11 years
    @Flup good point - the app was started using sudo and I wasn't logged in as root when running netstat.