From 4d24e9b47e0b7a45d47990dd51d4c8010cd5b72b Mon Sep 17 00:00:00 2001 From: Jim Richter Date: Thu, 21 Jan 2021 12:40:08 +0000 Subject: [PATCH] feat: add free port (fp) function --- configs/zsh/functions.zsh | 3 ++- configs/zsh/functions/fp.zsh | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 configs/zsh/functions/fp.zsh diff --git a/configs/zsh/functions.zsh b/configs/zsh/functions.zsh index 6b5698b..977a91b 100644 --- a/configs/zsh/functions.zsh +++ b/configs/zsh/functions.zsh @@ -2,4 +2,5 @@ source $(dirname "$0")/functions/co.zsh; source $(dirname "$0")/functions/fx.zsh; source $(dirname "$0")/functions/start.zsh; source $(dirname "$0")/functions/wp.zsh; -source $(dirname "$0")/functions/y.zsh; \ No newline at end of file +source $(dirname "$0")/functions/y.zsh; +source $(dirname "$0")/functions/fp.zsh; \ No newline at end of file diff --git a/configs/zsh/functions/fp.zsh b/configs/zsh/functions/fp.zsh new file mode 100644 index 0000000..4e894f6 --- /dev/null +++ b/configs/zsh/functions/fp.zsh @@ -0,0 +1,29 @@ +#Helps stop processes which use ports + +function fp(){ + + PORT=$1; + + if [ "$(which lsof)" = "lsof not found" ]; then + echo "lsof not installed" + else + PIDS="$(lsof -t -i:$PORT)" + + AMOUNT=$(wc -w<<<$PIDS); + + if [ $AMOUNT = 0 ]; then + + echo "No processes use port $PORT"; + + else + + echo "Killing $AMOUNT process(es) using port $PORT"; + + echo "$PIDS" | while IFS= read -r pid ; do + kill -9 $pid; + done + + fi + fi + +} \ No newline at end of file