Skip to content

Instantly share code, notes, and snippets.

@TheAmazingPT
Created July 30, 2018 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheAmazingPT/936b3daba53de99b39a0f814c5e8cacc to your computer and use it in GitHub Desktop.
Save TheAmazingPT/936b3daba53de99b39a0f814c5e8cacc to your computer and use it in GitHub Desktop.
How to find the corresponding directory of a script, which runs a process on a specific port.
#!/usr/bin/env bash
# Run this script, after you gave it a name like portfinder, added
# it to your path runtime and gave it an execution flag:
# portfinder 3000
# Required system tools:
# bash, lsof and pwdx
if [[ -z $1 ]]
then
echo "Please provide a portnumber!"
echo "Example: portfinder 8080"
exit 1
fi
PORT=$1
PID=$( lsof -Fp -i:$PORT | head -n1 | cut -d 'p' -f 2 )
PROC=$( ps -p $PID -o comm= )
DIR=$( pwdx $PID | cut -d ':' -f 2)
echo "$PORT is used by $PROC ($PID) in $DIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment