Django Development Server, How to Stop It When It Run in Background

django development server, how to stop it when it run in background?

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...

How to stop the Django development server on windows

Open Resource Monitor in the Command Prompt using the command resmon.
At the "Listening Ports", find the port you're using.
See Resource Monitor here.
In my case, is Port 8000.
Get the PID, in this example is 20132.
Open Task Manager, go to Tab Details, find the PID and end the task.
See Task Manager here

Django: Make the server continue to run without using runserver

You can keep it running in background without hanging up.
Just use below command.

nohup python manage.py runserver 0.0.0.0:8080 &

To kill

Find pid running on port 8080

netstat -nlp | grep :8080

and then after you get pid

kill pidnumber

PyCharm Django previous project`s server still runs after opening and running another project

On Windows:

  1. Open CMD
  2. Run command netstat -o to get list of running processes with open ports.
  3. Find the development server process by its address (for example http://127.0.0.1:8000/). You need to remember PID number.
  4. Run command Taskkill /PID #### /F(for example Taskkill /PID 26356 /F) to kill the process. Where #### is your running development server PID number.
  5. Enjoy! Now the server is not running anymore after the PyCharm was closed.

On linux:

django development server, how to stop it when it run in background

Running executable backgroung program inside server

You can do it with subprocess

template.html

<form action="{% url 'run_sh' %}" method="POST">
{% csrf_token %}
<button type="submit">Call</button>
</form>

urls.py

from django.urls import path

from . import views

urlpatterns = [
path('run-sh/', views.index, name='run_sh')
]

views.py

import subprocess

def index(request):
if request.POST:
subprocess.call('/home/user/test.sh')

return render(request,'index.html',{})

test.sh

#!/bin/sh
echo 'hello'

Based on answers to this question

Appropriate signal to kill django development server

Put a dash in front of the process, this should kill the process group.

 kill -s SIGINT -4189

How do I keep my Django server running even after I close my ssh session?

Meet screen.

Connect through ssh, start screen. This open a virtual console emulator on top of the one provided by ssh. Start your server there.

Then press Ctrl-a, then d. This detach the screen session, keeping it running in the background.

To [R]e-attach to it, use screen -r.

If screen is not installed and you can't install it, you can also start an application in the background by adding a & to the command, as you tried. But you should not close the terminal window then ; just disconnect, with the bash command exit, or Ctrl-d.

The advantage of screen is that you can still read the output from the server, in case there is an error or anything.

Screen is a really powerful tool, with many more commands. You can add a new virtual window with Ctrl-a, then c (for Create) ; switch through windows with Ctrl-a, then n (next) or p (previous), ...

But you need it to be installed to use it. Since you seem to have root access, this shouldn't be a problem.

EDIT: tmux is another great solution for the same use-case.

Quit Django dev server but process doesn't stop

Django development server have multiple threads, so when closing main process, there might be some running threads in background. It happens when there is some request being processed (some long-term request can hang or if you're using websockets or something, connection might prevent closing thread).

Check if all of your requests are properly closed before closing your server and it should shut down properly.



Related Topics



Leave a reply



Submit