108

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.

tchrist
  • 78,834
  • 30
  • 123
  • 180
Cyclone
  • 14,839
  • 23
  • 82
  • 114

8 Answers8

157

Short version which you can pass to kill command:

lsof -i:80 -t
Laurynas
  • 3,829
  • 2
  • 32
  • 22
131

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
Noam Yizraeli
  • 4,446
  • 18
  • 35
stanwise
  • 2,392
  • 1
  • 15
  • 21
17

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

gavenkoa
  • 45,285
  • 19
  • 251
  • 303
11

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

  • 2
    `netstat: 80: unknown or uninstrumented protocol` used the `80` (nginx) port for testing purpoes. Not worked. – Cyclone Mar 24 '12 at 23:39
4

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.

Debashish Sen
  • 696
  • 5
  • 12
3

Since sockstat wasn't natively installed on my machine I hacked up stanwise's answer to use netstat instead..

netstat -nlp | grep -E "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\:2000" | awk '{print $7}' | sed -e "s/\/.*//g""
dafky2000
  • 74
  • 10
3

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.

Glavin001
  • 1,276
  • 2
  • 17
  • 32
  • 1
    Here is the function I use to do it: `function kill-listener { lsof -i:$1 -t | xargs kill -9 }` Using your example of port 8089: `kill-listener 8089` – Hurricane Hamilton Jun 21 '17 at 14:00
2

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

Jan Wilmans
  • 665
  • 6
  • 10