lsof -i :8080 | awk '{print $2}' | tail -n 1 | xargs kill
lsof
lists open files (ls-o-f, get it?). lsof -i :8080
lists open files on address ending in :8080. The output looks like this
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
chrome 2619 qymspace 149u IPv4 71595 0t0 TCP localhost:53878->localhost:http-alt (CLOSE_WAIT)`
We pipe this input through awk
to print column 2 using the command awk '{print $2}'
to produce the output:
PID
2533
To remote the word PID
from this output we use tail -n 1
to grab the last row 2533
,
We can now pass this process id to the kill
command to kill it.
lsof -i :8080 | awk 'NR > 1 {print $2}' | xargs --no-run-if-empty kill
lsof -i :8080 | awk '{l=$2} END {print l}' | xargs kill