Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine "lsof -i :port" and "kill pid" in bash

How do i combine these two commands in the bash:

lsof -i :port
kill pid

The first one returns the PID i want to kill to release the port. The second one kills the returned PID.

I am doing this because I don´t know of any way to kill a jetty webserver within the Netbeans IDE on OSX. Is there a way?

like image 932
Oliver Avatar asked Oct 13 '15 11:10

Oliver


People also ask

How do you kill port lsof?

Use the kill command in combination with the lsof (list of open files) command to kill process listening to a particular port. As described in a previous post, the lsof command helps us identify which process is listening to a particular port - Just pass the result of the lsof command to the kill command.

How do I kill multiple ports?

Open a CMD window in Administrator mode by navigating to Start > Run > type cmd > right-click Command Prompt, then select Run as administrator. Use the netstat command lists all the active ports. To kill this process (the /f is force): taskkill /pid 18264 /f.


2 Answers

You can use $():

kill $(lsof -t -i:port)
like image 94
Eugene Soldatov Avatar answered Oct 20 '22 04:10

Eugene Soldatov


You can use

kill -9 `lsof -t -i:port`
like image 28
El Fadel Anas Avatar answered Oct 20 '22 04:10

El Fadel Anas