Skip to content

Instantly share code, notes, and snippets.

@bikemule
Created June 3, 2022 22:59
Show Gist options
  • Save bikemule/dab9cce5c0be6bb9e5fc24565c811279 to your computer and use it in GitHub Desktop.
Save bikemule/dab9cce5c0be6bb9e5fc24565c811279 to your computer and use it in GitHub Desktop.
bash function to find a
# Find what port a program is using
function portfinder() {
# Find out what port a program is using.
# First and only argument is the process name
# used to grep output of ps command
# Usage: portfinder name_of_process
# Example output:
# node 87825 user 35u IPv4 0xaffd45cf13a62c85 0t0 TCP *:62883 (LISTEN)
# first grep searches for first arg passed to portfinder, $1
# second grep filters out the line containing the first grep
# awk grabs only the field containing the PID
local PID=$(ps aux | grep "$1" | grep -v grep | awk '{print $2}')
# lsof lists files/processes with corresponding open ports, grep for the PID
lsof -nP -iTCP -sTCP:LISTEN | grep "$PID"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment