1

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 {}
  • 2
    save sed, awk and sort with: kill -kill $(sudo netstat -nlpt | grep -oPm1 '2020.*/\K.*') – dessert Dec 6 '18 at 7:05
4

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)
  • But first I want to check the port hang by someone, and if it is true then kill it. – Janith Dec 7 '18 at 6:12
  • Well , this will actually check all the process using the port initially and will kill all process attached to it – Sachu Shaji Abraham Dec 7 '18 at 6:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.