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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Category

CLI, linux, unix

Tags

, ,