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...
Related
I have setup a Django project on a virtual environment on my PC. When using the command
python manage.py runserver 0.0.0.0:8000
Bit Bash stops doing anything and I have to end the program to start over. I have waited several minutes and when I end the session, a dialogue says:
Processes are running in session:
WPID PID COMMAND
14904 1534 c:\Users\mine\AppData\Loca
Close anyway?
I have looked at every related question to this and tried every solution but I cannot get this to work, on or off the virtual environment.
Not sure if this applies, but I also noticed that in my task manager, python3.9.exe appears twice when trying to start the server. The status says running, and the PIDs are different numbers.
This is because something already running on port 8000. You can run the Django server by hosting on another port like 8080 but, you can also kill the already running task on the port. Follow the link to know how exactly you can kill any running process.
It is highly likely that another application is running using the port 8000. Try running the server using another port, say 8088 and share if the same issue persists.
To run on specific port kill all pids which is showing you on Git bash
(ps -ef)... kill them using (kill -9 pid_no)... then run your runserver command.
Ex:
ps -ef
kill -9 123
I ended up paying someone to fix this. Here is the conversation I had with the person who found the issue. Hope this helps someone out there.
Also, I noticed sometimes it still does freeze up on the watching for file changes with StateReloader until I go the browser and type in http://localhost:8000/. Once I do that the rest of the text loads and my browser shows The install worked successfully! Congratulations!
it works, what did you do?
- We have existing processes on port 8000.I think, you had the server running and kept trying over and over. So, I killed process using 8000 port.
- Next, I run Django project again.
- So, now it's working well.
ok. Can you tell me what you did at the start to make it work? Or was it always working?
- I started git bash session again when I started. Maybe git bash session had problems.
But this was going on for a day, I restarted the computer several times and restarted git bash. Same issue.
You must have done something.
- Sorry, I didn't do anything except restart git bash
how did you restart it?
- right click top of window, select NEW
How do I turn server off?
- press Ctrl + C
- And you didn't quit the server exactly before. Maybe you closed git bash directly and server was still live
- So, we had many 8000 ports.
Ok. How can I see what is on that port now?
- You can use this cli
- netstat -ano | findstr :<PORT>
- We don't have running 8000 port now.
I have never found solutions to this problem even with killing ports, changing port etc.
It never display the following part:
Starting development server at http://127.0.0.1:8000/
However, when I use the Git Bash terminal include into vscode, it displays the url.
Terminal > New Terminal > then:
Result :
python manage.py runserver 127.0.0.1:8080
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 have created a script to build my Django project which is called by my Jenkins CI, every time a push is made.
The script runs just fine if I run it manually, but fails to start the web server when it is ran automatically.
No errors are thrown, but the last line of the script:
nohup python manage.py runserver 0:9000 > /dev/null 2>&1 &
has absolutely no effect.
I am 100% sure that the script is ran as jenkins user, under my virtualenv, so that's not the problem. Also, permissions are not a problem, I have checked. Like I said, no error is thrown, so I don't really know what is happening.
Any ideas ?
So, thanks to tomrom95 I found the solution : adding BUILD_ID=dontKillMe in front of the command fixed everything. It's kind of funny.
Here is the link to a more complete answer to why this didn't work and why it works now.
I am running web py like below from terminal.
$ python app.py 8080
now how do i keep it running even when I close the terminal?
Is there any way to keep this server running.
Thank you
you can use nohup for that.
$ nohup python app.py 8080 &
It will stay open even if you close the terminal. You'll need to use kill and send some signal to it to close it.
I think that this is a question belonging to another stackexchange site (serverfault?).
Anyway, the answer is: run it in a screen:
screen python app.py 8080
Detach from the screen using the key combination:
Ctrl+a+d
Attach to the screen again (to see ouput from your process)
screen -r
Quit screen:
Ctrl+c
You can use supervisord. It is intended to do such tasks (and much more). Documentation is pretty clear, so it should not be a problem to set up it. See how to run supervisord.
Also do not use nohup since it will not do anything like autorestarting server after failure or after reboot.
In a Bourne-shell compatible shell (Bash, Bourne, Korn):
python app.py 8080 &
I just see that you wrote that you want to close the terminal. Apparently my answer won't work then.
I am having difficulty running Django on my Ubuntu server. I am able to run Django but I don't know how to run it as a service.
Distributor ID: Ubuntu
Description: Ubuntu 10.10
Release: 10.10
Codename: maverick
Here is what I am doing:
I log onto my Ubuntu server
Start my Django process: sudo ./manage.py runserver 0.0.0.0:80 &
Test: Traffic passes and the app displays the right page.
Now I close my terminal window and it all stops. I think I need to run it as a service somehow, but I can't figure out how to do that.
How do I keep my Django process running on port 80 even when I'm not logged in?
Also, I get that I should be linking it through Apache, but I'm not ready for that yet.
Don't use manage.py runserver to run your server on port 80. Not even for development. If you need that for your development environment, it's still better to redirect traffic from 8000 to 80 through iptables than running your django application as root.
In django documentation (or in other answers to this post) you can find out how to run it with a real webserver.
If, for any other reason you need a process to keep running in background after you close your terminal, you can't just run the process with & because it will be run in background but keep your session's session id, and will be closed when the session leader (your terminal) is terminated.
You can circunvent this behaviour by running the process through the setsid utility. See your manpage for setsid for more details.
Anyway, if after reading other comments, you still want to use the process with manage.py, just add "nohup" before your command line:
sudo nohup /home/ubuntu/django_projects/myproject/manage.py runserver 0.0.0.0:80 &
For this kind of job, since you're on Ubuntu, you should use the awesome Ubuntu upstart.
Just specify a file, e.g. django-fcgi, in case you're going to deploy Django with FastCGI:
/etc/init/django-fcgi.conf
and put the required upstart syntax instructions.
Then you can you would be able to start and stop your runserver command simply with:
start runserver
and
stop runserver
Examples of managing the deployment of Django processes with Upstart: here and here. I found those two links helpful when setting up this deployment structure myself.
The problem is that & runs a program in the background but does not separate it from the spawning process. However, an additional issue is that you are running the development server, which is only for testing purposes and should not be used for a production environment.
Use gunicorn or apache with mod_wsgi. Documentation for django and these projects should make it explicit how to serve it properly.
If you just want a really quick-and-dirty way to run your django dev server on port 80 and leave it there -- which is not something I recommend -- you could potentially run it in a screen. screen will create a terminal that will not close even if you close your connection. You can even run it in the foreground of a screen terminal and disconnect, leaving it to run until reboot.
If you are using virtualenv,the sudo command will execute the manage.py runserver command outside of the virtual enviorment context, and you'll get all kind of errors.
To fix that, I did the following:
while working on the virtual env type:
which python
outputs: /home/oleg/.virtualenvs/openmuni/bin/python
then type:
sudo !!
outputs: /usr/bin/python
Then all what's left to do is create a symbolic link between the global python and the python at the virtualenv that you currently use, and would like to run on 0.0.0.0:80
first move the global python folder to a backup location:
mv /usr/bin/python /usr/bin/python.old
/usr/bin/python
that should do it:
ln -s /usr/bin/python /home/oleg/.virtualenvs/openmuni/bin/python
that's it! now you can run sudo python manage.py runserver 0.0.0.0:80 in virtaulenv context!
Keep in mind that if you are using postgres DB on your developement local setup, you'll probably need a root role.
Credit to #ydaniv