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:
- Open CMD
- Run command
netstat -o
to get list of running processes with open ports. - Find the development server process by its address (for example
http://127.0.0.1:8000/
). You need to remember PID number. - Run command
Taskkill /PID #### /F
(for exampleTaskkill /PID 26356 /F
) to kill the process. Where####
is your running development server PID number. - 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
Error! C:\File\Example.Db Is Not Utf-8 Encoded Ipython Notebook
Python - How to Extract Elements from an Array Based on an Array of Indices
How to Assign Values to a Numpy Array as a Function of Index
Regex to Append Some Characters in a Certain Position
How to Track the Number of Times a Function Is Called
Python - How to Sort a List of Alpha and Numeric Values
How to Convert Float into Hours Minutes Seconds
Iterate Over Worksheets, Rows, Columns
Subtract One Dataframe from Another Excluding the First Column Pandas
How to Share Data Between a Parent and Forked Child Process in Python
How to Disable Pylint Unused Import Error Messages in VS Code
Get Max Value Comparing Multiple Columns and Return Specific Values
Pyspark Data Frame Converting False and True to 0 and 1
Python: Create 50 Objects Using a for Loop
Python Json Serialize a Decimal Object
How to Shut Down a Python Simplehttpserver
How to Remove Grid Lines on Image in Python
How to Extract Text from an Existing Docx File Using Python-Docx