Created
June 16, 2020 11:06
-
-
Save radlinskii/967e9b992add47e8105db933ec002123 to your computer and use it in GitHub Desktop.
script for killing a process running on given 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
# 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage output: