Recent I am running Django project on terminal using this command:
python manage.py runserver 0.0.0.0:80
But Server is stopped by closing terminal, So I need to run server in background.
How can I solve this issue?
You can use the nohup command, so your command runs without the terminal and all the outputs from the program will go to the file nohup.out (in the same directory you ran the command).
Use like so:
nohup python manage.py runserver 0.0.0.0:80
You can use screen to run a program in background.
This should answer your question
You may try:
python manage.py runserver 0.0.0.0:80 &
"&" puts the executed command in background and sets init as it's parent process when you close the terminal
You can run nohup, an output of the command will not be set what will not be created in the command (where or was executed)
nohup python manage.py runserver 0.0.0.0:8000
If you are using some automation software, in order not to crash the deployment, add an '&' at the end of the command, like this
nohup python manage.py runserver 0.0.0.0:8000 &
Related
I am using different Servers alongside Django Server. For Example MongoDB server and Celery[command]
I want to ask that how can I execute other CMD commands automatically whenever I start "**
python manage.py runserver
**"
Depends on what OS you use, on my Ubuntu for local development I do this:
Create .sh script. For example start_project.sh with this code:
cd /path/to/project
source /venv/bin/activate
python manage.py runserver & celery -A project worker --loglevel=debug
And then just run bash start_project.sh
Also you can add more commands to start separated by &
You should write a shell script which contains commands to start each service and then use it to get your projects running. For example here is a sample:
sudo service mongodb start
celery -A worker appname.celery
python manage.py runserver 0.0.0.0:80 > /dev/null 2>&1 &
Due to you use the term CMD I guess you use a Windows based OS. I would then say that you probably have the mongoDB service installation? (otherwise reinstall mongoDB as Service).
By defualt set to autostart (changable to non-autostart). If you changed the service for mongoDB to manual starting method, then you could start it in CMD as
net start mongoDB
I do not use/know what "Celery" is but quick google made it sound as some sort of message que. Which in my opinion should be or at least should have a service installation in which case you should use that and then use autostart/manual as described for mongoDB.
I haven't yet been able to get Apache working with my Django app, so until I do get it working, I'm using runserver on my Linux server in order to demo the app. The problem is that whenever I close the SSH connection to the server, runserver stops running. How can I keep runserver running when I say put my laptop to sleep, or lose internet connectivity?
P.S. I'm aware that runserver isn't intended for production.
Using Screen
You can run runserver in a screen session. After you detach from that session it will keep running. Login via ssh and start a screen session via
screen
It will look like your usual terminal. Now, run the server
python manage.py runserver 8080
After this, you can detach from the session using Ctrl+a Ctrl+d. Now your app should be available even after you quit your ssh session.
If you want to cancel the runserver, you can re-atach to your screen session. Get a list of existing sessions with
screen -ls
There is a screen on:
10989.pts-1.hostname (Detached)
1 Socket in /run/screens/S-username.
Then, you can reatach with the command
screen -R 10989
Using nohup
Again, after login into your server, start the runserver with
nohup python manage.py runserver 8080 &
All output the runserver writes (like debug info and so on) will be written to a file called nohup.out in the same folder.
To quit the server after using nohup you need to keep the process id (pid) shown or find the pid afterwards with ps, top or any other tool.
Since runserver isn't intended to be ran in production/only for development there is no way to to this built in.
You will need to use a tool like tmux/screen or nohup to keep the process alive, even if the spawning terminal closes.
$ nohup python manage.py runserver &
nohup makes command ignore hangup signal and & puts in to background disconnecting from stdout
If you are SSH'ing through to your server and starting Django there, consider use of a program on the server such as tmux. This will allow your server-side shell process to remain alive after disconnection, and you can reattach on your next login with a simple
tmux attach
command.
I am reading this tutorial: Installing and Configuring Graphite and Statsd on an Ubuntu 12.04 VPS
and I am working to automatize everything is possible then there is one step of this tutorial that is giving me crazy:
Next, we will configure the Graphite database. Go to the Graphite
webapp directory and run the database script:
cd /opt/graphite/webapp/graphite/
sudo python manage.py syncdb
As you see, we have to run the manage.py and when I run syncdb ask about a creation of superuser. How can I avoid that? I would like to run these sending all parameters to make an automatic script.
Any ideas?
You can use the --noinput argument to disable those prompts for the syncdb command.
--noinput
Use the --noinput option to suppress all user prompting, such as “Are you sure?” confirmation messages. This is useful if django-admin.py is being executed as an unattended, automated script.
When I type and execute the command python manage.py runserver localhost:8080 directly in the terminal everything works fine but when I place the command inside a shell script I get the following error:
The script contains the following:
Does anyone know why this is happening?
There are spaces in the script after runserver.
You should remove these to ensure you're only running the desired command without any indication of host / port.
I installed Django-socketio because it seems like the best way to implement chat by myself. The problem I have is that when I run
python manage.py runserver_socketio host:port it runs and I can't close the terminal or it will stop working, how can I get around that?
I solved the same issue with command nohup.
So you start the server with nohup python manage.py runserver_socketio host:port & (& to start in backgroud). The result is stored in a nohup.out file...