Skip to content

Instantly share code, notes, and snippets.

@ronakjain2012
Created February 10, 2024 16:36
Show Gist options
  • Save ronakjain2012/bbe7e0ab1b22d375f05b3b4d8bf9aeed to your computer and use it in GitHub Desktop.
Save ronakjain2012/bbe7e0ab1b22d375f05b3b4d8bf9aeed to your computer and use it in GitHub Desktop.
Kill the process by process id script
#!/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