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