find open ports and safely kill them — #bash utility for @21 developers

Added this simple bash script to two1helpers on Github:

https://github.com/fredzannarbor/two1helpers/blob/master/portkiller.sh

It lists processes running on an open port and prompts to be sure that you want to safely kill them.  This is useful when you are repeatedly opening and closing ports as part of developing 21 server applications.

#!/bin/bash
# usage ./portkiller.sh $1
PORT_NUMBER=$1
echo "searching for processes listening on $PORT_NUMBER which are"
lsof -i tcp:${PORT_NUMBER} | xargs -d\n
read -r -p "Are you sure you want to kill these? [y/N] " response
case $response in
    [yY][eE][sS]|[yY])
        lsof +c14 -i tcp:${PORT_NUMBER} | awk 'NR!=1 {print $2}' | xargs kill
        ;;
    *)
        echo "ok, never mind!"
        ;;
esac
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s