Skip to content

Instantly share code, notes, and snippets.

@gerrard00
Forked from tobym/pwdx_for_mac.bash
Last active November 1, 2018 10:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gerrard00/a08918dddc65a7bb2d814756b28ed2a1 to your computer and use it in GitHub Desktop.
Save gerrard00/a08918dddc65a7bb2d814756b28ed2a1 to your computer and use it in GitHub Desktop.
pwdx for mac. Usage: pwx pid also portpwdx that finds the pwd for the process that owns a port. Usage: portpwdx port
function pwdx {
# note: NF is the number of columns, so $NF gives us the last column
lsof -a -p $1 -d cwd -n | tail -1 | awk '{print $1, "\t", $NF}'
}
function portpwdx {
# pid=$(lsof -i :5858 | tail -1 | perl -pe 's/[^\s]+\s+([^\s]+)\s.*/$1/')
pid=$(lsof -i :$1 | tail -1 | perl -pe 's/[^\s]+\s+([^\s]+)\s.*/$1/')
if [[ ! -z $pid ]]; then
echo $pid
pwdx $pid
return 0
else
echo "No process listening to port $1"
return -1
fi
}
@gerrard00
Copy link
Author

gerrard00 commented Jun 29, 2016

Damn, what's holding that port open? Oh, ok netstat says node. But wait, which node process?

Ah:

portpwdx 8080

will show:

12345
node /user/me/projects/my-cool-project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment