867

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
  • 732,580
  • 175
  • 1,330
  • 1,459
Tampa
  • 75,446
  • 119
  • 278
  • 425

30 Answers30

1751

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)
armanexplorer
  • 93
  • 2
  • 5
zellio
  • 31,308
  • 1
  • 42
  • 61
  • 11
    @FrancescoGramano: you don't want to use -9 unless you have to. – zellio Jun 01 '15 at 16:55
  • 2
    when `sudo kill` can't kill the process, @putra.koreng 's method works well – Fangxin Dec 15 '17 at 01:07
  • 3
    if above is not working then try this: https://stackoverflow.com/a/50515868/9339242 – Arayan Singh May 24 '18 at 18:29
  • 1
    Is this OS independent or linux specific? If Linux specific then, even this command works `os.system("fuser -k 8080/tcp");` – Ridhuvarshan Jul 25 '18 at 12:04
  • This closes my browser entirely https://stackoverflow.com/a/19621849/4579897 works – Winnipass Jan 18 '19 at 14:50
  • `sudo fuser 9001/tcp -k` in case the above command does not work for you. – Armin Jul 23 '19 at 19:18
  • You may want to run the inner part (i.e. `sudo lsof -t -i:9001`) to check to see what processes are returned. For me, it returned PIDs for both the server _and_ clients, which is probably what happened to @AdamWinnipass. – webelo Sep 23 '19 at 10:26
  • sudo kill -9 `sudo lsof -t -i:8000` – Ashish Sahu Aug 25 '20 at 17:12
  • 1
    Does not work anymore. Only comes up with a help command for both commands – Epic Speedy Mar 28 '21 at 17:31
  • 1
    @EpicSpeedy It shows you the help command when it doesn't find the process that you want to kill. That means you probably chose a wrong port or the process stop already – Youssef N Mar 28 '22 at 11:00
  • It is highly recommended *not* to use `kill -9` unless you have to. `kill -9` will just rip the process out of the process table and clean up what the OS knowns about (e.g. memory). Instead use `kill -HUP`, if that doesn't work then `kill -INT`, then if necessary `kill -9`. The first two will give the process a chance to clean up its own matters before exiting. – Philip Kearns Oct 26 '22 at 14:33
  • Also works on mac much better than others I've tried – Max Carroll Feb 02 '23 at 11:41
  • just for myself `sudo kill -9 $(sudo lsof -t -i:3000)` – Sasha Kos Apr 05 '23 at 21:23
412

you can use

fuser -n tcp -k 9001 

see more details in wikipedia

fjsj
  • 10,995
  • 11
  • 41
  • 57
putra.koreng
  • 4,145
  • 1
  • 12
  • 3
157

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 -9 6782
Lemon Kazi
  • 3,308
  • 2
  • 37
  • 67
Mohit Singh
  • 5,977
  • 2
  • 24
  • 25
119

The best way to kill a port in the Ubuntu terminal is with the fuser command; e.g.:

fuser -k 9001/tcp

This sends SIGKILL by default.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Vishnu S Babu
  • 1,570
  • 1
  • 12
  • 23
39

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
  • 3,192
  • 3
  • 16
  • 35
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
  • 957
  • 11
  • 25
24

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
  • 87,526
  • 38
  • 249
  • 254
19
killport <port>

Add this code in your CLI and use this alias.

killport () {
  PID=$(sudo lsof -t -i:$1)
  sudo kill -9 ${PID}
}
FalcoGer
  • 2,278
  • 1
  • 12
  • 34
Towfiqul Islam
  • 456
  • 6
  • 14
15

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
김태은
  • 107
  • 1
  • 1
  • 10
Prakash Nagaraj
  • 533
  • 4
  • 12
14

sudo kill -9 $(sudo lsof -t -i:9001) worked for me or you can use

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

replace 9001 with port number you want.

12

Use this:

sudo kill -9 $(lsof -t -i:9001)
Pang
  • 9,564
  • 146
  • 81
  • 122
delpha
  • 931
  • 1
  • 8
  • 23
11

Just enter the following command in the terminal

kill -9 $(lsof -t -i:3000)
Nishant Kumar
  • 691
  • 1
  • 11
  • 31
11

Lets consider If you want to kill a port 3000.

Step 1: Run this command, which will give u its PID.

sudo lsof -t -i:3000

This returned me PID as 291141

Step 2: Run this command to kill the port using PID.

sudo kill -9 291141

Boom!!! Port killed

ANOOP NAYAK
  • 473
  • 5
  • 8
6

you can use following command in terminal
fuser -k 3000/tcp

Naveen Kumar
  • 987
  • 11
  • 11
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
  • 319
  • 1
  • 6
  • 15
Guna Sekaran
  • 553
  • 4
  • 5
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
  • 2,189
  • 1
  • 15
  • 26
5

If I was you,I would use

fuser -k -n tcp PORT
kill -9 PID
Loveli
  • 51
  • 1
  • 1
5

Just add an alias to your .bashrc / .zshrc:

  1. Open new terminal
  2. nano .bashrc
  3. scroll down and add an alias: alias kp='kill_process_by_port() { sudo kill -9 $(sudo lsof -t -i:"$1"); }; kill_process_by_port "$@"'
  4. ctrl + x +y
  5. source .bashrc
  6. kp 9000
knaitas
  • 59
  • 3
  • 7
  • lsof: unacceptable port specification in: -i : lsof 4.93.2 latest revision: https://github.com/lsof-org/lsof latest FAQ: https://github.com/lsof-org/lsof/blob/master/00FAQ latest (non-formatted) man page: https://github.com/lsof-org/lsof/blob/master/Lsof.8 usage: [-?abhKlnNoOPRtUvVX] [+|-c c] [+|-d s] [+D D] [+|-E] [+|-e s] [+|-f[gG]] [-F [f]] [-g [s]] [-i [i]] [+|-L [l]] [+m [m]] [+|-M] [-o [o]] [-p s] [+|-r [t]] [-s [p:s]] [-S [t]] [-T [t]] [-u s] [+|-w] [-x [fl]] [--] [names] Use the ``-h'' option to get more help information. kill: (8000): No such process – texas697 May 06 '23 at 21:38
  • Fixed: `alias kp='kill_process_by_port() { sudo kill -9 $(sudo lsof -t -i:"$1"); }; kill_process_by_port "$@"'` – knaitas Jun 24 '23 at 21:58
4

Try this:

lsof -i :port

or 

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

or

kill -9 <pid>

or 

sh killport 8000
Ranvijay Sachan
  • 2,407
  • 3
  • 30
  • 49
3
sudo kill `sudo lsof -t -i:9001`

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

davidblumer
  • 103
  • 1
  • 3
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,786
  • 1
  • 13
  • 30
2

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.
  • 15,774
  • 6
  • 47
  • 88
Vadim
  • 158
  • 1
  • 11
2
  • get PID of process running on port 3000:

    lsof -i tcp:3000

  • kill th process

    kill -9 process_id

Example:
lsof -i tcp:3000
(PID in output = 5805)
kill -9 5805

use sudo if required

shekhar677
  • 89
  • 4
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
  • 2,104
  • 1
  • 19
  • 28
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
  • 2,685
  • 2
  • 16
  • 16
1

When using newer versions of Ubuntu you won't have netcat anymore, instead you use ss

ss -lptpn | grep 9001   
sudo kill <replace-with-process-id>
Francisco Cardoso
  • 1,438
  • 15
  • 20
1

sudo netstat -lpn |grep :8080

than get the PID of this process

kill -9

1

You can also use xargs. Just run the following,

sudo lsof -t -i:443 | xargs sudo kill -9

Avi
  • 11
  • 1
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 23 '22 at 06:40
1

On my ubuntu-22.04 machine, this worked to kill port 8000

fuser -k 8000/tcp