Run Python application on DigitalOcean [duplicate] - python

This question already has answers here:
Are a WSGI server and HTTP server required to serve a Flask app?
(3 answers)
Closed 9 months ago.
I need help to start my Python application on DigitalOcean droplet. I set up all the settings and now can run my python file. But if I close the Ubuntu console - my loop or any other code (sending requests for example) finish. I want to start a Flask server which will receive webhooks all time when machine works (24/7). How can I start the process without working console on my Desktop? The question is not about Flask, only about endless working program. Thanks.

You could use screen or nohup to have your python script running 24/7.
screen allows you to create a terminal session and detach from it, leaving the process started on it running. You can install it on Ubuntu with the command below. See this tutorial or this one for more information.
sudo apt-get update sudo apt-get install screen
nohup allows you to do the same. It basically runs a command ignoring hangup signals, not stopping when you log out. Unlike screen, nohup is normally already installed by default on Ubuntu. See its manual page for more information about it.
Finally, in case you are interested in knowing more about the differences between screenand nohup, they were explained in this post.

Related

How to run Dash App in background/offline [duplicate]

This question already has answers here:
Are a WSGI server and HTTP server required to serve a Flask app?
(3 answers)
Closed 1 year ago.
I am new to dash app development and created a app which is running in the linux server. It is available to all users in our intranet only when I trigger and online. Once I logoff it is not accessible. How to schedule the app to run continuously even when I am offline. Any responses would be appreciated.
currently it is serving with below command
python app.py
I cant deploy in Heroku due to security restrictions. Docker also unavailable. Any other option would be appreciated.
Regards,
Sudheer
From this description, I guess that you
SSH into the server
Run python app.py
At this point, the app is available
Log off from the SSH connection (e.g. exit command)
At this point, the app is not available any more
If so, this is because the command you run during the SSH session will be terminated when you log off from the session.
There are several ways to keep your app running after you log off. For example,
Run nohub python app.py &.
Run tmux and run python app.py in the tmux window. Then Ctrl+b+d to close the tmux window.
In either way, the app will keep running after you log off from the SSH connection. If my guess about your situation is wrong, please elaborate on your situation more in detail. For example, tell us the series of command you run and what happens with them.
In particular, the term "offline" is not clear in this context. If you are completely offline and not connected even to the intranet, then there is no chance that you can use the app running on the server.

Does Python(FLASK) have somethink like nodaemon? [duplicate]

This question already has answers here:
Auto reloading python Flask app upon code changes
(14 answers)
Closed 1 year ago.
with nodaemon code changes will be displayed without rebooting the server, does Python(FLASK) have somthing like that?
Yes. Nodemon will automatically restart the server everytime you update your code. Flask has a similar feature. To enable it you have to set your environment to development. To do so type the following at the command line:
set flask_env=development
The above works when using a windows cmd line. If you are using bash try:
$ export FLASK_ENV=development
Then start your server.
See: https://flask.palletsprojects.com/en/master/server/

How to access terminal Python process running on server from other script

Consider situation:
I have an Ubuntu server with installed Python, tensorflow and other libs.
My code is python script, that load several models, some of them pretrained vectors .bin, some files from server folders, etc.
When i run script in terminal it launch interactive session, where i input some text and script output me back (like chatbot). During answer it call my Ai models (Tensorflow, keras).
Question: how do i access this running session from other python script? I mean i want use it as a function: to send text and receive answer back.
And of course i need to run this terminal session in background for long time.
I read this and similar answers, but not sure is that right solution (seems not a full):
In Linux, how to prevent a background process from being stopped after closing SSH client
What i am asking, commonly is done by REST server with API that expose and then this api is called from a external code. But there is no API wotking: Tensorflow throw errors when run via Flask (was not able to fix).
If you want your script stays up after closing ssh session, add & disown at the end of your execution command and it will run in background.

How do I get a python program to remain active on the server after I close putty? [duplicate]

This question already has answers here:
How to run Node.js as a background process and never die?
(14 answers)
Closed 6 years ago.
I wrote a program a few months ago in python/JavaScript that uses bottle, but when I moved it to my website (hosted on Amazon Web Service), it works when I type: sudo python2.7 moviegame.py, but when I close Putty, the website doesn't show my game.
How do I get the server to keep running my program without relying on my computer?
Typically you would use something like supervisor http://supervisord.org/ or some other process control system. There are many out there.

How do I run Django as a service?

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

Categories

Resources