terminate simple python server wont shut down with ctrl + C

Lukasz picture Lukasz · Nov 3, 2013 · Viewed 15.3k times · Source

I ran a simple web server using

python -m simpleHTTPServer 8888 &.

it starts well. Then used ctrl+C to attempt to terminate it, and I go to

http://localhost:8888/ 

and the server is still running. Am I doing something wrong?

I am using zsh for terminal, not sure if this has anything to do with it.

Answer

Robin Krahl picture Robin Krahl · Nov 3, 2013

It’s because of the & that causes the command to be run in background. After you started the process, you get the process id. Using that id, you can kill the process:

$ python &
[1] 5050
$ kill -15 5050
[1]+  Angehalten              python

If sending a SIGTERM signal (-15) does not work, you can use SIGKILL (-9).

Edited to use SIGTERM instead of SIGKILL, see @starrify’s comment.