Skip to content

Instantly share code, notes, and snippets.

@radlinskii
Created June 16, 2020 11:06
Show Gist options
  • Save radlinskii/967e9b992add47e8105db933ec002123 to your computer and use it in GitHub Desktop.
Save radlinskii/967e9b992add47e8105db933ec002123 to your computer and use it in GitHub Desktop.
script for killing a process running on given port
# kill process running on given port
# argument: port number
# example usage:
# > killp 3000
function killp() {
local tmp
local exit_status
echo "looking for process running on port: $1"
tmp=$(lsof -i :"$1" | awk '{ print $2}' | grep "^[0-9][0-9]*$")
exit_status=$?
if [ $exit_status -ne 0 ]; then
echo "\e[41munable to find the process!\e[0m"
else
echo "trying to kill process: $tmp"
kill -9 "$tmp"
exit_status=$?
if [ $exit_status -ne 0 ]; then
echo "\e[41munable to kill the process!\e[0m"
else
echo "\e[30;48;5;82msuccessfully killed the process!\e[0m"
fi
fi
echo "exit status: $exit_status"
return $exit_status
}
@radlinskii
Copy link
Author

Example usage output:
Screenshot 2020-06-16 at 13 07 00

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