Skip to content

Latest commit

 

History

History
40 lines (31 loc) · 1.85 KB

2018-10-16-script-to-close-a-port-of-macos.md

File metadata and controls

40 lines (31 loc) · 1.85 KB
title featured description image date_orig date draft tags
Script to Close a Port on MacOS
false
A very handy Bash alias for web developers to close an open port on macOS.
./../../assets/blog/photo-1512380924987-27e737876395.jpg
2018-10-16 16:00:00 -0400
2018-10-16
false
devops

A big part of my current role is building and maintaining multiple online publications using similar or multiple port numbers. It's not uncommon for me to have 10 or 12 VSCode editors open on across my desktops. As a result, from time to time, I need to simply kill one application's use of a port to start up another one. Rather than toggle through multiple windows, I opted to have a method to quickly shut down an application associated with a port number.

There is also a use case where a watch process (Gulp or Grunt) crashes leaving the required port open as part of a background thread. (I have encountered this with node-sass on more than one occasion).

Important Note

Some System Level Applications use specific ports to function. Be Cautious when using this command as you could inadvertently shut down your system.

Demo

Kill the Port

Simply add to the bottom of your .bashrc or .zshrc file in your home directory (sh: vi ~/.bashrc).

killport() {
    PID=$(lsof -ti ":$1")
    if [ ! -z "$PID" ]; then
        echo "PORT: $1"
        echo "PID:  $PID"
        kill -9 $PID
        echo "OK!"
    else
        echo "No Process Found running Port $1"
    fi
}

And Reload the Window or the Source (source ~/.bashrc).

To use the script, simply enter killport #### with the port number you need to shutdown.