Languages
[Edit]
EN

Linux - kill process listening on specific port

4 points
Created by:
Hiba-Tate
458

In this short article, we would like to show how to kill process that listens on a specific port in Linux.

Quick solution:

kill -9 $(lsof -t -i:8080 -sTCP:LISTEN)

Where: 8080 is a number of used port by a process that we want to kill.

 

How does it work?

lsof part:

  • lsof returns details of opened files - in Linux¬†common politics is to represent all things¬†by files,
  • we want to get only PID, so -t parameter is useful¬†to do it - returning only process ID (PID),
  • with -i:8080 we are able to find information about processes that use¬†8080 port,
  • with¬†-sTCP:LISTEN we will check only listening TCP sockets.

kill part:

  • kill¬†program stops process on kernel level (because of -9¬†parameter) using¬†PID¬†from lsof command.

More safe way example

Alternatively, for save we can do all steps manually to be sure that the proper program was killed.

Simple steps:

  1. display processes that use port 8080,
    Command:
    lsof -w -i:8080

    Output:

    COMMAND   PID  USER  FD   TYPE  DEVICE  SIZE/OFF  NODE NAME
    node    16908  john  21u  IPv4  220968       0t0  TCP *:8080 (LISTEN)
  2. kill the selected process (in out case it is PID 16908 with (LISTEN) note),
    Command:
    kill -9 16908
    Output:
    Killed
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Linux

Native Advertising
ūüöÄ
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

‚̧ԳŹūüíĽ ūüôā

Join