Determine the process pid listening on a certain port

前端 未结 8 1720
孤城傲影
孤城傲影 2021-01-29 23:30

As the title says, I\'m running multiple game servers, and every of them has the same name but different PID and the port number. I would

相关标签:
8条回答
  • 2021-01-29 23:43

    Syntax:

    kill -9 $(lsof -t -i:portnumber)

    Example: To kill the process running at port 4200, run following command

    kill -9 $(lsof -t -i:4200)
    

    Tested in Ubuntu.

    0 讨论(0)
  • 2021-01-29 23:46

    Short version which you can pass to kill command:

    lsof -i:80 -t
    
    0 讨论(0)
  • 2021-01-29 23:51

    on windows, the netstat option to get the pid's is -o and -p selects a protocol filter, ex.: netstat -a -p tcp -o

    0 讨论(0)
  • 2021-01-29 23:58

    I wanted to programmatically -- using only Bash -- kill the process listening on a given port.

    Let's say the port is 8089, then here is how I did it:

    badPid=$(netstat --listening --program --numeric --tcp | grep "::8089" | awk '{print $7}' | awk -F/ '{print $1}' | head -1)
    kill -9 $badPid
    

    I hope this helps someone else! I know it is going to help my team.

    0 讨论(0)
  • 2021-01-29 23:59

    netstat -nlp should tell you the PID of what's listening on which port.

    0 讨论(0)
  • 2021-01-30 00:03

    netstat -p -l | grep $PORT and lsof -i :$PORT solutions are good but I prefer fuser $PORT/tcp extension syntax to POSIX (which work for coreutils) as with pipe:

    pid=`fuser $PORT/tcp`
    

    it prints pure pid so you can drop sed magic out.

    One thing that makes fuser my lover tools is ability to send signal to that process directly (this syntax is also extension to POSIX):

    $ fuser -k $port/tcp       # with SIGKILL
    $ fuser -k -15 $port/tcp   # with SIGTERM
    $ fuser -k -TERM $port/tcp # with SIGTERM
    

    Also -k is supported by FreeBSD: http://www.freebsd.org/cgi/man.cgi?query=fuser

    0 讨论(0)
提交回复
热议问题