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