Search code examples
unixportfreebsdpid

Determine the process pid listening on a certain port


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 like to match the PID of the server which is listening on certain port, and then I would like to kill this process. I need that in order to complete my bash script.

Is that even possible? Because it didn't find yet any solutions on the web.


Solution

  • The -p flag of netstat gives you PID of the process:

    netstat -l -p
    

    *use sudo if showing - instead of PID

    Edit: The command that is needed to get PIDs of socket users in FreeBSD is sockstat. As we worked out during the discussion with @Cyclone, the line that does the job is:

    sockstat -4 -l | grep :80 | awk '{print $3}' | head -1