Created
July 30, 2018 14:20
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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