Buy @ Amazon

Kill process listening/bound to specific port


Step 1: Identify the process
    For this you can use either of the command below:
lsof -w -n -i tcp:<port_number>
where,
-w implies suppression of warning messages
-n option inhibits the conversion of network numbers to host names for network files. Inhibiting conversion may make lsof run faster. It is also useful when host name lookup is not working properly.
-i[i] option selects the listing of files any of whose Internet address matches the address specified in i. If no address is specified, this option selects the listing of all Internet and x.25 (HP-UX) network files.
      If -i4 or -i6 is specified with no following address, only files of the indicated IP version, IPv4 or IPv6, are displayed. (An IPv6 specification may be used only if the dialects supports IPv6, as indicated by "[46]" and "IPv[46]" in lsof's -h or -? output.) Sequentially specifying -i4, followed by -i6 is the same as specifying -i, and vice-versa. Specifying -i4, or -i6 after -i is the same as specifying -i4 or -i6 by itself.
    Multiple addresses (up to a limit of 100) may be specified with multiple -i options. (A port number or service name range is counted as one address.) They are joined in a single ORed set before participating in AND option selection.
      An Internet address is specified in the form (Items in square brackets are optional.):
      [46][protocol][@hostname|hostaddr][:service|port]
      where:
            46 specifies the IP version, IPv4 or IPv6 that applies to the following address. '6' may be be specified only if the UNIX dialect supports IPv6.  If neither '4' nor '6' is specified, the following address applies to all IP versions.

            protocol is a protocol name - TCP or UDP.

            hostname is an Internet host name.  Unless a specific IP version is specified, open network files associated with host names of all versions will be selected.

            hostaddr is a numeric Internet IPv4 address in dot form; or an IPv6 numeric address in colon form, enclosed in brackets, if the UNIX dialect supports IPv6.  When an IP version is selected, only its numeric addresses may be specified.

            service is an /etc/services name - e.g., smtp - or a list of them.

            port is a port number, or a list of them.

Sample Command Line Output:
karthik@cloud:~/MyRubyProjects/dashboard$ lsof -w -n -i tcp:3000
COMMAND  PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
ruby    2969 karthik    5u  IPv4  68913      0t0  TCP *:3000 (LISTEN)

Alternatively, you may use the command below:
netstat -anp | grep :<port_number>
where,
-a displays all active connections and the TCP and UDP ports on which the computer is listening.
-n displays active TCP connections, however, addresses and port numbers are expressed numerically and no attempt is made to determine names.
-p Linux: Process : Show which processes are using which sockets (similar to -b under Windows) (you must be root to do this)

Sample Command Line Output:
karthik@cloud:~/MyRubyProjects/dashboard$ netstat -anp | grep :3000
(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 0.0.0.0:3000            0.0.0.0:*               LISTEN      2969/ruby    

Step 2: Kill the process

kill -9 <procecss_id_number>

Eg:
karthik@cloud:~/MyRubyProjects/dashboard$ kill -9 2969