Lukasz
Lukasz

Reputation: 97

terminate simple python server wont shut down with ctrl + C

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.

Upvotes: 2

Views: 16205

Answers (5)

jhschwartz
jhschwartz

Reputation: 300

TLDR: If sending a SIGKILL doesn't work, as a last resort try killall python

I ran into a similar problem in which the localhost server would simply change to another pid even after I gave it a SIGKILL. The only thing that worked for me, and would probably work as a last resort for anyone else experiencing this would be to simply run in bash: killall python.

Upvotes: 0

Rahul Arora
Rahul Arora

Reputation: 1036

It's because of &. It is now a background process which you cannot kill by ctrl+c.

As it is server, I recommend you to use &. To kill off the server, do -> ps aux | grep simpleHTTPServer to find the process id and then do kill -9 pid

Upvotes: 0

Robin Krahl
Robin Krahl

Reputation: 5308

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.

Upvotes: 7

starrify
starrify

Reputation: 14731

I don't know whether you need &. here. However in most of the shells (of course zsh is included) the symbol & mean to start this task in the background, which means your CTRL+C is actually not received by the application.

Try remove the &. and see what would happen.

EDITED: See @RobinKrahl's answer for how to terminate the process by sending a signal to it using kill. However for terminating a finely working process I suggest use SIGTERM (signal number 15) instead of SIGKILL (signal number 9).

Upvotes: 0

Deepankar Bajpeyi
Deepankar Bajpeyi

Reputation: 5859

CTRL+C sends SIGINT. You should send a kill signal using kill -9 pid to kill the process

Upvotes: 0

Related Questions