How to find out the PID of a process by its PORT

Took me a while getting in the right direction. Took our linux guru 20 secs to finish it. Here you go:

netstat -tulpn 2>/dev/null | awk '$4~/:::YOURPORTHERE/ {gsub("/.*","",$NF); print $NF}'

Wanna use a variable inside? Thats the way:

PORT=8080
netstat -tulpn 2>/dev/null | awk -v p=":::$PORT" '$4==p {gsub("/.*","",$NF); print $NF}'
Advertisements

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