I remember when I was first getting started with web programming, sometimes I would accidentally
close the terminal window that was running my development server.
Unfortunately, this meant that when I opened a new terminal window to start
up a new server, I would run into an error because port 3000
or 8080
was already in use.
To solve this problem, I would restart my computer because I knew that that
would solve the problem, but I always felt like there must be a more narrowly tailored approach.
If you find yourself in this situation, here are two solutions that I use:
To see all of the processes that are listening on TCP ports, run the following:
$ lsof -iTCP -sTCP:LISTEN
To make sure that you kill the right process, you can run the following command to learn more about the process in question.
$ ps -p ${PID}
Once you’re sure that it’s the right process, you can terminate it by running:
$ kill ${PID}
Assuming that you’re running a rails development server, you can run
the following command to see all of the ruby processes that are running.
If you’re working on a django or express project, then replace ruby
with python
or node
respectively.
$ ps -ae | grep "ruby"
Once you identify the one that is running your web server you can run:
$ kill ${PID}
I hope this will help you avoid some unnecessary frustration if you end up accidentally closing the wrong terminal window. Happy hacking.