Close process by port number with one line

Recently I encountered myself executing so many times the following unix commands:

1: $ netstat -anp | grep :port_number
2: (search manually the output for the PID that is using the given :port_number)
3: $ kill -9 :pid

Bored, I challenged my friend Alisson to build a command to perform those three steps, in one single line.

He came up with the following solution:

kill -9 $(sudo netstat -anp | grep :[port_number] | awk '{print $7}' | cut -d '/' -f 1)

Congratulations man! You deserve a very cold Heineken!

About these ads