Skip to content

Instantly share code, notes, and snippets.

@dominikkaegi
Last active February 23, 2019 15:03
Show Gist options
  • Select an option

  • Save dominikkaegi/b0d2e7e2c509a38293c29a9a979fd421 to your computer and use it in GitHub Desktop.

Select an option

Save dominikkaegi/b0d2e7e2c509a38293c29a9a979fd421 to your computer and use it in GitHub Desktop.
Utilities function to find and kill a process on a port easily
# Implemented and tested on MacOS
# Add utilties to ~./bash_profile to make them usable in your terminal
# ------------------- PORT UTILTIES ----------------------
# Find PID of process on a specific port
# param portNumber
# example: "findPortProcess 8080"
findPortProcess() {
portProcess="$(lsof -n -i4TCP:$1 | grep LISTEN)"
if [ "$portProcess" ]
then
echo $portProcess
else
echo "No Process on Port:$1"
fi
}
# Kills Process on a Port
# param portNumber
# example: "killPort 8080"
killPort() {
processPID="$(lsof -ti tcp:$1)"
if [ "$processPID" ]
then
kill $processPID
echo "Killed Process on Port:$1"
else
echo "No Process on Port:$1"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment