61

I use a Cloud server to test my django small project, I type in manage.py runserver and then I log out my cloud server, I can visit my site normally, but when I reload my cloud server, I don't know how to stop the development server, I had to kill the process to stop it, is there anyway to stop the development?

2
  • 4
    This is a good question. Yes, all of us know how to kill a process but when a service provides a start option, you assume you don't know something... and look for a stop. It is rather an oversight that django breaks this paradigm.
    – KateYoak
    Oct 5, 2015 at 20:39
  • 1
    @KateYoak Well, I sent it to Django developers.
    – Brambor
    Sep 7, 2020 at 4:16

11 Answers 11

105

The answer is findable via Google -- and answered in other forums. Example solution is available on the Unix & Linux StackExchange site.

To be explicit, you could do:

ps auxw | grep runserver

This will return the process and its respective PID, such as:

de        7956  1.8  0.6 540204 55212 ?        Sl   13:27   0:09 /home/de/Development/sampleproject/bin/python ./manage.py runserver

In this particular case, the PID is 7956. Now just run this to stop it:

kill 7956

And to be clear / address some of the comments, you have to do it this way because you're running the development server in the background (the & in your command). That's why there is no "built-in" Django stop option...

4
  • 1
    Can you bring the job to the foreground using 'fg' on the command line? I toggled mine to bg then back to fg. Then CONTROL-C stops the job altogether. When I start my server, it prints a few lines of text, the last being "Quit the server with CONTROL-C." February 01, 2018 - 20:31:32 Django version 2.0.1, using settings 'mysite.settings' Starting development server at 127.0.0.1:8000 Quit the server with CONTROL-C. Feb 1, 2018 at 20:34
  • @the_cat_lady that is a good suggestion, too! I would say that warrants its own answer, since it's another way to do it...
    – user
    Feb 1, 2018 at 21:06
  • Unfortunately, Django launches a child process, which makes two process, which makes it hard to kill from a script. Especially because killing the parent does not kill the child. May 22, 2019 at 6:06
  • "The answer is findable via Google" --> I searched on Google and found this current page, from Google :)
    – Patapoom
    Oct 12, 2022 at 8:53
50

One liner..

pkill -f runserver
3
  • 3
    awesome. Thats short and sweet. Oct 17, 2019 at 18:26
  • 3
    Watch out, in case multiple 'runserver' processes are running!!! This will kill all running "runserver" processes.
    – amolbk
    Jan 30, 2020 at 8:40
  • @amolbk, what is the disadvantage of doing like that ?
    – Utku Can
    Mar 15, 2023 at 11:59
15

Try this

lsof -t -i tcp:8000 | xargs kill -9
1
  • thanks very much, the only solution that works.
    – Weilory
    Nov 14, 2021 at 6:09
7

Ctrl+c should work. If it doesn't Ctrl+/ will force kill the process.

6

well it seems that it's a bug that django hadn't provided a command to stop the development server . I thought it have one before~~~~~

1
  • 1
    My ISP provides a very limited shell that kicks me out on some commands, like kill and even ps :) Though I may easily execute python manage.py ... It's a pity to file a support ticket each time I need to execute a single command. So I agree – it's a must to be able to stop the server
    – p.boiko
    Oct 31, 2017 at 15:12
5

We can use the following command.

-> netstat -ntlp

then we will get number of process running with PID, find our python server PID and Kill process.

-> kill -9 PID

For example:
enter image description here

3

As far as i know ctrl+c or kill process is only ways to do that on remote machine. If you will use Gunicorn server or somethink similar you will be able to do that using Supervisor.

1
  • 1
    ctrl+c will not stop a hearty infinite loop.
    – Marc
    May 7, 2015 at 19:32
2

This worked for me on windows.

Use the below command to list all connections and listening ports (-a) along with their PID (-o).

netstat -a -o

Find the PID of the process

Then use this to kill the process

taskkill /PID PUT_THE_PID_HERE /F
1

From task manager you can end the python tasks that are running. Now run python manage.py runserver from your project directory and it will work.

1

Programmatically using a .bat script in Command Prompt in Windows:

@ECHO OFF
SET /A port=8000
FOR /F "tokens=5" %%T IN ('netstat -ano ^| findstr :%port%') DO (
    SET /A processid=%%T
    TASKKILL /PID %%T /F
)

gives

SUCCESS: The process with PID 5104 has been terminated.

-4

You can Quit the server by hitting CTRL-BREAK.

1
  • Not if it’s in the background. May 22, 2019 at 5:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.