How to kill a process on a port on ubuntu

前端 未结 27 2293
天涯浪人
天涯浪人 2020-12-02 03:05

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
         


        
27条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 03:50

    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
    

提交回复
热议问题