Have you ever tried to run a server on a specific port only to find out that it was already in use? I ran into this issue just the other day. The problem was I kept using CTRL-C
to stop the server and it wasn’t gracefully shutting down. I’ll share the short-term fix and then the long-term one.
Short-term fix
The short term fix is typing the following into the terminal, where 8080
represents whatever port you are running the server on:
lsof -i :8080
This should display the process id (PID) which you could then use in the following, replacing `123` with the actual id:
kill -9 123
For the curious, lsof
stands for “LiSt Open Files”, and you can see many more uses for the command in this linux blog post.
Long-term fix
The long term fix is to implement a graceful shutdown. Here is an example for Node. You basically tell the process to listen for SIGTERM
and SIGINT
events and manually shut down the server.