Skip to content

Instantly share code, notes, and snippets.

@yanil3500
Last active October 24, 2019 22:00
Show Gist options
  • Save yanil3500/e5b8ffbdfda70442a4ed59b2646f32fa to your computer and use it in GitHub Desktop.
Save yanil3500/e5b8ffbdfda70442a4ed59b2646f32fa to your computer and use it in GitHub Desktop.
[Stop Using Port] Bash script for terminating a process using a specific port #bash #tcp #ports

1. Go to /usr/local/bin/

2. Make file named stop and paste the below code in it.

3. Run chmod +x stop to make the script executable.

#!/bin/bash
touch temp.text
lsof -n -i4TCP:$1 | awk '{print $2}' > temp.text
pidToStop=`(sed '2q;d' temp.text)`
> temp.text
if [[ -n $pidToStop ]]
then
kill -9 $pidToStop
echo "Congrats!! $1 is stopped."
else
echo "Sorry nothing running on above port"
fi
rm temp.text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment