Kill zombie processes that opened a port

If you are having issues with running a script because a process already has the port open, in this error example, it’s port 3000

Error: listen EADDRINUSE :::3000

We can create a bash script and execute it passing in the first parameter as the port.

This script will run and list processes using the port we pass and attempt to kill it. It will pipe the output to /dev/null so it will silently execute and terminate. For this example we will save our script as killScript.sh and make sure we change the permissions using chmod u+x killScript.sh

#!/usr/bin/env bash
kill -9 $(lsof -i:$1 -t) 2> /dev/null


We can run this script by running it and passing in our port ( 3000 in this example )

sh killScript.sh 3000

Jon

Innovative, entrepreneurial and positively unsatisfied mind that constantly pushes the tech boundaries to create new solutions and devices that change people’s lives. Throughout my career, I have had the opportunity of having a multi-disciplinary experience, which led me to wear the hats of: Architect, IoT Consultant (Internet of Things Consultant), Developer, Designer, Engineer, Server Admin, Consultant and Project Manager – to name a few. Based on this diversified background, I get projects done by following smart ideas and well-planned goals. In a world which is getting more unconventional by the day, I use unconventional ways, and a very healthy dose of integrity, to create devices and solutions that help companies go from a steady present to a dynamic future.

You may also like...