2

I have a command to check the port and kill specific pid running on that port. But I want to modify it to,

  1. Check port is busy or available
  2. If the port is busy then kill the pid

How can I do this?

Code is given below.

sudo netstat -nlpt | grep 2020 | awk '{print $7}' | sed s+/.*++g | sort -u | xargs -i kill -kill {}
1
  • 2
    save sed, awk and sort with: kill -kill $(sudo netstat -nlpt | grep -oPm1 '2020.*/\K.*')
    – dessert
    Dec 6, 2018 at 7:05

1 Answer 1

5

You can make use of this command to delete port in Ubuntu.

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

The lsof command or list of open files with -i flag will find out all the running process of specific port and gives the corresponding process Id. The kill option will terminate the process with pid given from the lsof command.

So if you want to kill process running on port 3000, you will have to execute the following command

sudo kill $(sudo lsof -t -i:3000)
2
  • But first I want to check the port hang by someone, and if it is true then kill it.
    – Janith
    Dec 7, 2018 at 6:12
  • Well , this will actually check all the process using the port initially and will kill all process attached to it Dec 7, 2018 at 6:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .