Created
February 10, 2024 16:36
-
-
Save ronakjain2012/bbe7e0ab1b22d375f05b3b4d8bf9aeed to your computer and use it in GitHub Desktop.
Kill the process by process id script
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
#!/bin/bash | |
if [[ $# -eq 0 ]] ; then | |
echo 'Usage: ./$0 <port> <port> <port> ...' | |
exit 0 | |
fi | |
cnt=0 | |
for port in $@; do | |
while :; do | |
processid=$(sudo lsof -i :$port | tail -1 | awk '{print $2}') | |
if [[ ! -z "$processid" ]]; then | |
echo "Killing process $processid" | |
sudo kill -9 $processid | |
cnt=$((cnt+1)) | |
else | |
break | |
fi | |
done | |
done | |
exit $cnt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment