Kurt Peek
Kurt Peek

Reputation: 57381

How to determine which process is using a port in Linux

I'm currently running RethinkDB on its default port, because if I point my browser to localhost:8080 I see the RethinkDB web interface:

enter image description here

I'd like to close RethinkDB and re-open it on another port using the --port-offset argument. However, so far I haven't been able to close it from within Python using the conn.close() method.

Instead, I'd like to try a brute-force approach by simply killing the process using that port. I tried to determine which process that is by using netstat, but that doesn't bring up any results:

kurt@kurt-ThinkPad:~$ netstat -a | grep 8080
kurt@kurt-ThinkPad:~$ 

How can I kill the RethinkDB process to make the port available again?

Upvotes: 5

Views: 8979

Answers (5)

MD Shahrouq
MD Shahrouq

Reputation: 617

Awesome answer @codespy . I did a bash file and wrote it like this.

# !/bin/bash
kill $(lsof -t -i  :8000)

And save it and script file.

And made executable by

$chmod +x script_filename

Now I run just by entering

./script_filename

Upvotes: 0

whatspoppin
whatspoppin

Reputation: 383

There is a simple solution, some Unix/Linus has the command ss which is the new generation command similar to netstat, you could simply type the following:

ss -ltnp

-p list process number

-l list listening sockets

-t list tcp sockets

-n list port number instead of conventional name (21 instead of ssh)

check the man page for more info

Upvotes: 0

Harshit Bansal
Harshit Bansal

Reputation: 117

To find out the pid or all the info about particular port as by which accused

sudo lsof -i :PORT_NUMBER

To kill the process or event

kill -9 PID

example:-

harshit@harshit:~/Desktop/edwin-diaz/cms$ lsof -i :4111
COMMAND  PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
node    9215 harshit   33u  IPv6 297470      0t0  TCP *:4111 (LISTEN)
harshit@harshit:~/Desktop/edwin-diaz/cms$ kill -9 9215

Upvotes: 1

Kurt Peek
Kurt Peek

Reputation: 57381

Following Klaus D.'s comment, I determined the process using netstat -nlp:

kurt@kurt-ThinkPad:~$ netstat -nlp | grep 8080
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 127.0.1.1:8080          0.0.0.0:*               LISTEN      2229/rethinkdb  
tcp        0      0 127.0.0.1:8080          0.0.0.0:*               LISTEN      2229/rethinkdb  
tcp6       0      0 ::1:8080                :::*                    LISTEN      2229/rethinkdb 

The arguments stand for

  • numeric (show numerical addresses instead of trying to determine symbolic host, port or usernames)
  • listening (show only listening sockets (these are omitted by default))
  • program (show the PID and name of the program to which each socket belongs)

respectively.

Upvotes: 0

vivkv
vivkv

Reputation: 1001

1.  lsof -i:8080
2.  kill $(lsof -t -i:8080)
or
2 . kill -9 $(lsof -t -i:8080)

Upvotes: 13

Related Questions