DEV Community

F1LT3R
F1LT3R

Posted on

Down Boy! (Kill processes by Port)

Need to kill processes by port? Try this...

Cute dog

Setup

Save this bash script as downboy to your bin directory:

#!/bin/bash
for PORT in $@
do
    PID=$(lsof -i :$PORT | awk 'FNR ==2 {print $2}')
    echo "kill -9 $PID"
    kill -9 $PID
done
Enter fullscreen mode Exit fullscreen mode

Make downboy executable:

chmod +X ./downboy
Enter fullscreen mode Exit fullscreen mode

Usage

Now you can easily close any number of processes by their port:

sudo downboy 80 8080 3000
Enter fullscreen mode Exit fullscreen mode

Bye-bye.

Top comments (2)

Collapse
 
ferricoxide profile image
Thomas H Jones II

Back in the mists of time, I recall that either the IRIX or Solaris implementation of fuser had the ability to kill processes by port.

Collapse
 
mlennox profile image
Mark Lennox

Will save a lot of ps -ef | grep on my machine - I needed this, thanks!