Django: Make the server continue to run without using runserver - python

I have started using django_channels on my server. But if I want the websocket to work, I have to use:
python manage.py runserver 0.0.0.0:8080
And the server runs and I can connect ws://myip:8080
But, as soon as I do ctrl+c. It quits i.e, I am unable to connect on ws://myip:8080 anymore.
I want to be running continuously.

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

Related

Run flask application on linux server with nohup

I am trying to run my flask application on my linux server using nohup , but each time i run my flask application with nohup , my server will require me to kill with cntrl + c in order for me to do other things
assuming i have 2 file in my server
path(home/app/)
flask_app.py [ which is my flask application ]
flk.sh
inside my run.sh i have included
nohup python /home/app/flask_app.py
when i run my shell script in my server which is sh flk.sh
my system will hang as per shown in below image if i dont exit it as its running and all activity will go inside nohup.out
how can i run nohup on my flask application without me having to use cntrl + c to exit to run other commands.
You usually want to use nohup command arg1 arg2 &. You're just missing the ampersand.
Briefly, nohup prevents the OS from killing your job, & sends the job in the background, so it isn't tied to your current shell.

How to update python code and see changes live using daphne, Django Channels?

I just made some changes over 1 python file in my production server,
then tested the changes using "runserver" command:
python3 manage.py runserver 0.0.0.0:3031
The changes are done correctly, then I try to see the same changes in production but using websockets with Django Channels, but the result seems to be that the server is running the old code.
nohup daphne -b 0.0.0.0 -p 3031 asgi:channel_layer &
nohup python manage.py runworker &
What could be the reason, is there any code cache?, what can I do to refresh the code?
I found that the solution was to kill all "python runworkers" processes, and then restart the server with daphne and runworker.
sudo pkill python
nohup daphne -b 0.0.0.0 -p 3031 asgi:channel_layer &
nohup python manage.py runworker &

Why i cannot start service web?

Help me with this error please.
ERROR: for web Cannot start service web: driver failed programming
external connectivity on endpoint semestral_dj01
(335d0ad4599512f3228b4ed0bd1bfed96f54af57cff4a553d88635f80ac2e26c):
Bind for 0.0.0.0:8000 failed: port is already allocated ERROR:
Encountered errors while bringing up the project.
Go to Terminal and run command:
lsof -i:8000
Where 8000 is the port number.
The result will be like:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
Python 123456 user ab type 123 000 TCP 0.0.0.0:8000
Now run command in terminal:
kill -9 <PID>
like
kill -9 123456
Then again run your server and the issue will be resolved.
The way i resolved this was by stopping the containers in execution and executing the one I wanted to start.
Use this command into your CMD for stop containers:
docker stop $(docker ps -a -q)
In the case you may want to delete them use this:
docker rm $(docker ps -a -q)
This happen to me time to time in my dev environment. Usually I have to restart docker service to get it working.
I encountered a very similar error. In my case, I had recently upgraded the native nginx version on the Linux box. After the upgrade, nginx automatically started (I had not noticed). When I deployed a docker image with nginx, the 2 nginx instances were competing for the same port (native and docker).
I saw it with:
> sudo netstat -nl -p tcp | grep 443
tcp 0a 0 0.0.0.0:443 0.0.0.0:* LISTEN #####/nginx: master
tcp6 0 0 :::443 :::* LISTEN #####/nginx: master
It was a bit confusing since I was trying to get nginx to run, and it said nginx was using the port. After I had typed docker-compose down, I realized nginx was still using the port, even though the nginx container was destroyed. That made me realize that the native nginx had started up again, even though I didn't manually start it.
My error message:
Cannot start service <webserver>: driver failed programming external connectivity on endpoint <server_instance>_webserver (...<guid>...): Error starting userland proxy: listen tcp 0.0.0.0:443: bind: address already in use

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

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?
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...
One liner..
pkill -f runserver
Try this
lsof -t -i tcp:8000 | xargs kill -9
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~~~~~
Ctrl+c should work. If it doesn't Ctrl+/ will force kill the process.
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.
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:
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.
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
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.
You can Quit the server by hitting CTRL-BREAK.

how to restart a django app through fcgi

we run a django app on our server through this command.
python manage.py runfcgi host=127.0.0.1 port=8070 pidfile=/home/ubuntu/autoleg_webapp/autoleg_clients.pid --settings=PROD_Settings
I have made some changes to PROD_Settings.py file,i want to restart now, how would i do that?
Tour processe's PID is in the pidfile, so you should basically kill this PID.
To do so, execute the following in your shell:
kill `cat /home/ubuntu/autoleg_webapp/autoleg_clients.pid`
Thanks to the backticks, your shell will replace cat home/ubuntu/autoleg_webapp/autoleg_clients.pid with the PID that's in the file.

Categories