Kill a process
When you accidentally close a terminal but the process is still running and blocking a port, you can kill it with this command.
First get a list of process and search for listening ports
lsof -i -P | grep LISTEN
it should result in something like this
ControlCe 580 rohan 11u IPv6 0xbc8fe00eaebc28f5 0t0 TCP *:5000 (LISTEN)
rapportd 628 rohan 8u IPv4 0x84238893ff3ee7e0 0t0 TCP *:63947 (LISTEN)
rapportd 628 rohan 9u IPv6 0x745bde29f20d42ef 0t0 TCP *:63947 (LISTEN)
ruby 52285 rohan 6u IPv4 0xf1f440be40da1e31 0t0 TCP localhost:4000 (LISTEN)
Electron 61686 rohan 47u IPv4 0x56192f19875a781 0t0 TCP localhost:62587 (LISTEN)
manager 61722 rohan 29u IPv4 0x7c6940291f97d1cc 0t0 TCP localhost:51000 (LISTEN)
ckg_serve 61734 rohan 9u IPv4 0xcdabc1daa657b77e 0t0 TCP localhost:51010 (LISTEN)
node 83664 rohan 20u IPv6 0xf5ea5fc66cb80dd1 0t0 TCP *:3000 (LISTEN)
node 83665 rohan 20u IPv6 0xe1798e7c005a7103 0t0 TCP *:3002 (LISTEN)
which is of the format
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
now you can select the process by it’s PID
and terminate with kill -9 <PID>
(is same as kill -KILL <PID>
)
e.g. if I want to free up my port 3002
kill -9 83665
kill -9 <PID>
sends the “kill” signal which cannot be caught or ignored. The process will be forcibly shut down with no notification to the process, and no chance to do any cleanup what so ever.
PS: you might have to use sudo
if the process is running in elevated privilege.