Skip to content

Instantly share code, notes, and snippets.

@eftakhairul
Created April 25, 2020 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eftakhairul/784a060aec4fe262212436ea042e6a2d to your computer and use it in GitHub Desktop.
Save eftakhairul/784a060aec4fe262212436ea042e6a2d to your computer and use it in GitHub Desktop.
killport tool
#!/bin/bash
# Kill process on requested port
# Author: Md Eftakhairul Islam
if [[ $# -eq 0 ]]; then
echo "Port is not specified"
exit 1
fi
# get the port
port=$1
# get the process id
process_id=$(lsof -i :"${port}" | awk '{ print $2; }' | head -n 2 | grep -v PID)
if [[ process_id -gt 0 ]]; then
kill -9 $process_id
echo "Process: ${process_id} killed running on port ${port}"
else
echo "Sorry! No process found running on port ${port}"
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment