622

I am trying to kill a process in the command line for a specific port in ubuntu.

If I run this command I get the port:

sudo lsof -t -i:9001

so...now I want to run:

sudo kill 'sudo lsof -t -i:9001'

I get this error message:

ERROR: garbage process ID "lsof -t -i:9001".
Usage:
  kill pid ...              Send SIGTERM to every process listed.
  kill signal pid ...       Send a signal to every process listed.
  kill -s signal pid ...    Send a signal to every process listed.
  kill -l                   List all signal names.
  kill -L                   List all signal names in a nice table.
  kill -l signal            Convert between signal numbers and names.

I tried sudo kill 'lsof -t -i:9001' as well

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Tampa
  • 62,379
  • 105
  • 250
  • 388

28 Answers28

1207

You want to use backtick not regular tick:

sudo kill -9 `sudo lsof -t -i:9001`

If that doesn't work you could also use $() for command interpolation:

sudo kill -9 $(sudo lsof -t -i:9001)
Tanya Branagan
  • 381
  • 1
  • 7
  • 17
zellio
  • 25,079
  • 1
  • 38
  • 57
303

you can use

fuser -n tcp -k 9001 

see more details in wikipedia

fjsj
  • 10,501
  • 9
  • 36
  • 54
putra.koreng
  • 3,047
  • 1
  • 9
  • 3
105

FOR UBUNTU 1- Find what application/process is using the pro, type:

sudo netstat -lpn |grep :8080

and press Enter.

You will get an output similar to this one

tcp6       0      0 :::8080                 :::*                    LISTEN      6782/java

2- I have got the process Id, which is 6782, now this is the process that is using port 8080.

3- Kill the process, type: kill -p 6782

kill -p 6782
CrazyVideoGamez
  • 311
  • 3
  • 12
Mohit Singh
  • 5,163
  • 2
  • 19
  • 24
57

The best way to kill a port in ubuntu terminal is fuser -k 9001/tcp

Vishnu S Babu
  • 902
  • 7
  • 21
37

There are some process which does not shown using normal netstat command, so you have to check it using sudo.

Do sudo netstat -lpn |grep :8080. Process running by system does not show PID, to get PID of this process you will have to run it using sudo

And then killl the process using port 8080. You may have to use sudo here as well. So to kill this process use sudo kill -9 PID

User16119012
  • 937
  • 10
  • 25
25

To kill a process running on Port number 9001

sudo kill -9 $(sudo lsof -t -i:9001)


lsof   - list of files(Also used for to list related processes)

-t     - show only process ID

-i     - show only internet connections related process

:9001  - show only processes in this port number

kill   - command to kill the process

-9     - forcefully

sudo   - command to ask admin privilege(user id and password).

for more you can visit my blog

Arayan Singh
  • 2,574
  • 2
  • 11
  • 31
23

Use killport command :

sh killport 9001 

To Download shell ,you can use wget :

wget https://cdn.rawgit.com/abdennour/miscs.sh/e0aac343/killport
Abdennour TOUMI
  • 64,884
  • 28
  • 201
  • 207
22

just enter

    fuser -k -n tcp 3000

in terminal , where 3000 is your port number

Sai Kumar
  • 439
  • 3
  • 7
  • 1
    using fuser: Show which processes use the named files, sockets, or filesystems -k: Stands for a kill -n: Netstat tcp: That is a protocol to use your port 3000: Custom port here is the link: https://askubuntu.com/questions/207609/i-cant-kill-a-port-process – Kaushik shrimali Sep 05 '19 at 06:39
21

To kill on port service in ubuntu, Enter the below command in terminal

In Terminal :

sudo fuser -k port/tcp
sudo fuser -k 8001/tcp
Abhishek-Saini
  • 687
  • 6
  • 11
19

You may also use fuser to do that (sends SIGKILL by default): sudo fuser -k 9001/tcp

Dmitriusan
  • 8,724
  • 2
  • 31
  • 35
11

To kill the process based on port first we have to find out the relevant pid for given port and kill using that pid,

In my case i wanted to get the pid(process id) of 3000 port:

netstat -ltnp | grep -w '3000'

then find pid which is listening to tcp

tcp6       0      0 :::3000                 :::*                    LISTEN      29972/node  

you will get pid 29972

To kill this pid use below command

kill -9 29972

pseudo code for kill process based on port

 netstat -ltnp | grep -w 'PORT'

and

kill -9 PID
김태은
  • 65
  • 1
  • 10
Prakash Nagaraj
  • 413
  • 4
  • 12
9

Use this:

sudo kill -9 $(lsof -t -i:9001)
Pang
  • 8,605
  • 144
  • 77
  • 113
delpha
  • 731
  • 1
  • 5
  • 20
5

Kill PORT:

sudo is important to show process id.

$ sudo netstat -antlp | grep 45136
tcp      0      0 0.0.0.0:45136         0.0.0.0:*        LISTEN           **-** 

$ kill -9 45136
Lnux
  • 269
  • 5
  • 14
Guna Sekaran
  • 467
  • 3
  • 5
5

Use this for killing process which is running on port number "9001"

 kill $(lsof -t -i:9001)
arvind
  • 117
  • 1
  • 6
5

Use the command

 netstat -plten |grep java

used grep java as tomcat uses java as their processes.

It will show the list of processes with port number and process id

tcp6       0      0 :::8080                 :::*                    LISTEN      
1000       30070621    16085/java

the number before /java is a process id. Now use kill command to kill the process

kill -9 16085

-9 implies the process will be killed forcefully.

kumbhani bhavesh
  • 1,759
  • 1
  • 13
  • 22
5

sudo fuser -n tcp -k [port_number]

sudo lsof -t -i:[port_number]

Community
  • 1
  • 1
Javad
  • 51
  • 1
  • 3
4

If I was you,I would use

fuser -k -n tcp PORT
kill -9 PID
Loveli
  • 41
  • 1
3

Try this:

lsof -i :port

or 

sudo kill $(sudo lsof -t -i:8000)

or

kill -9 <pid>

or 

sh killport 8000
Ranvijay
  • 2,007
  • 2
  • 23
  • 42
3

It gives me an error OSError: [Errno 98] Address already in use. using the following method solved my error in Ubuntu.(VERSION="18.04.4 LTS (Bionic Beaver)")

$ sudo netstat -lpn |grep :5000
tcp        0      0 0.0.0.0:5000            0.0.0.0:*               LISTEN      25959/uwsgi   

$ fuser -k -n tcp 5000
5000/tcp:            25959
 

Hope it will help someone!

Ransaka Ravihara
  • 1,127
  • 1
  • 8
  • 22
2

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

can be used to kill the process. You can use any port number instead of 9001

Charuka Herath
  • 273
  • 2
  • 18
1
sudo kill `sudo lsof -t -i:9001`

you don't have to add the -9signal option

davidblumer
  • 83
  • 1
  • 3
1

Its two steps process:

  1. Know process id on port no. 8080 (can be any)
  2. Kill process of that id 8689 (can be different)

    fuser -n tcp 8080
    
    #o/p 8080/tcp    8689
    
    kill -9 8689
    
1

you can work this using node

npm install freethenport -g

then

node freethenport 9001
Adeojo Emmanuel IMM
  • 1,702
  • 1
  • 14
  • 25
1

Displays active TCP connections, ports on which the computer is listening.

netstat -tupln

It will list you all the connection along pid. Find and copy the pid and run the following command. Make sure you replace the with actual id in the following command.

kill -9 <copied pid>

-9 use to forcefully kill the connection.

Lalit Mohan
  • 1,408
  • 8
  • 12
1

Try sudo fuser PORT/tcp -k Replace PORT with the port you want to kill, for example 80

Armin
  • 985
  • 1
  • 12
  • 13
1

xargs can be a very useful command.

You can pipe commands like this

lsof -t -i :9001 | xargs sudo kill

What it does: it takes the output from the first command, in this case the process running on port, and will pass it to sudo kill.

TT.
  • 14,883
  • 6
  • 41
  • 77
Vadim
  • 46
  • 1
  • 4
-1

the below command will kill your running port

sudo kill -9 `sudo lsof -t -i:9001`
-2

sudo kill $(sudo lsof -t -i:1337)