How to stop flask app.run()? - python

I made a flask app following flask's tutorial. After python flaskApp.py, how can I stop the app? I pressed ctrl + c in the terminal but I can still access the app through the browser. I'm wondering how to stop the app? Thanks.
I even rebooted the vps. After the vps is restated, the app still is running!

CTRL+C is the right way to quit the app, I do not think that you can visit the url after CTRL+C. In my environment it works well.
What is the terminal output after CTRL+C? Maybe you can add some details.
You can try to visit the url by curl to test if browser cache or anything related with browser cause this problem.

Since you are using apache so in order to stop your app, you have to disable it by deleting .conf file from '/etc/apache2/sites-enabled/' folder and then restart the apache server. This will surely destroy your current running session.
$ cd /etc/apache2/sites-enabled/
$ sudo rm conf_filename.conf
$ sudo service apache2 restart
Try it and your site will be down. To enable it again, copy paste your file to '/etc/apache2/sites-available/' and run the following commands to enable it again.
$ sudo a2ensite conf_filename.conf
$ sudo service apache2 restart
Now your site will be live again.

Have you tried pkill python?
WARNING: do not do so before consulting your system admin if are sharing a server with others.

Related

how to make your python web app always running on ubuntu machine

I have a web app that I deployed to a machine that has ubuntu 20 installed
to be able to run the app I should open ssh to the ubuntu machine and then run this command
cd mywebapp
python3 app.py
it works successfully, but once I close the ssh console or reboot the machine or anything happens, it stopped and I have to repeat these commands
I tried to add it as a corn job to be run after machine reboot but it does not work
I post a question in the following link : run python app after server restart does not work using crontab
nothing work with me, and I have to make sure that this web app will always be running because it should be working to send push notification to mobile devices
can anyone please advice, I have been searching and trying for so many time
I'm not expert in it, but two solutions come in my mind:
1- Using systemd:
systemd can be responsible to keep services up.
You can write a custom unit for your app, and config it as a way to be up always.
This tutorial may be useful: writing unit
2- Using Docker:
When you have containerized app, you config it as to come up, on failure or anything like that.
Read about it here
What if you have the calling piece of Python script within a bash script and run that as a daemon:
Your bash script could like below (test.sh):
#!/bin/sh
cd desired/directory
python3 app.py
and you can run the bashscript like this by using nohup:
nohup ./test.sh 0<&- &>/dev/null &
You could refer this, if you want to store the outputs of nohup.

502 Bad Gateway after reloading supervisor

My Flask web application runs using nginx and gunicorn. I use supervisor to let my application run in the background. I always updated my files using Windows Power Shell and the command SCP. After i moved the new edited files, which are already existing on my Ubuntu server, to the server, i use the command sudo supervisorctl reload to restart the flask app to see the changes. But this time the flask app did not start and i only get 502 Bad Gateway. It does not matter how many times i reload the supervisor or restart nginx, i only get the error code 502.
The issue was a not installed module and a typing error in a configuration file.

Run a Flask app forever, even after reboot

I have a running Flask app that is working fine on a Linux (CentOS) server.
To make it work in the background I started it within a screen session. There is scheduled downtime for the server every night. The next day all screen sessions are gone. How can I keep my Flask app or screen sessions always up and running after reboots?
Han Solo gave a good answer in his comment: if you run it as a systemd service, it'll run on boot, restart if it crashes, and so on.
However, if you just need a quick-and-dirty solution for a couple days while you configure the service properly, you can use cron:
Run crontab -e to edit your crontab file
Add a line at the bottom that looks like this:
#reboot [put your command here]
Save and exit the editor
sudo reboot to test it and make sure it works
And you're done! The command will now run whenever the system reboots.

Odoo service restart and update using systemctl

Odoo service is started by systemctl start odoo. I am usin Centos. When I want to update my changed *.py code I used to do like this:
1. systemctl stop odoo
Then I update my module and database by useing this:
2. ./odoo.py -c openerp-server.conf -u <my_module_name> -d <database_name>
3. stop service by ctrl + c
4. systemctl start odoo
But it's realy long and uncomfortable way to update changes.
Is there a shorter way to do the same operations in shorter way?
Odoo with Service
You can make changes like this:
Stop the server: systemctl stop odoo
Start the server: systemctl start odoo. Here the .py are updated
If you also need to update xml or some translations you can press the Update button on the Odoo interface, on the module description form.
Note: There are modules to reload specific xml views. If you are interested in it I can take a look to check if I find one.
Odoo without Service
If you are developing on your local computer, you donĀ“t need to use systemctl. Just run Odoo directly with odoo.py and you can see the changes immediately:
./odoo.py -c openerp-server.conf -u <my_module_name> -d <database_name>
Autoreload Python Files
There is another option to reload python files when they have changed. Check this other answer:
Normally if you change your python code means, you need to restart the
server in order to apply the new changes.
--auto-reload parameter is enabled means, you don't need to restart the server. It enables auto-reloading of python files and xml files
without having to restart the server. It required
pyinotify. It is a Python
module for monitoring filesystems changes.
Just add --auto-reload in your configuration file. By default the
value will be "false". You don't need to pass any extra arguments.
--auto-reload is enough. If everything setup and works properly you will get
openerp.service.server: Watching addons folder /opt/odoo/v8.0/addons
openerp.service.server: AutoReload watcher running in the server log. Don't forget to install pyinotify package.
But in odoo 10 just add --dev=reload paramter
--dev=DEV_MODE Enable developer mode. Param: List of options
separated by comma. Options : all,
[pudb|wdb|ipdb|pdb], reload, qweb, werkzeug, xml
Failed to stop odoo.service: Access denied. See system logs and 'systemctl status odoo.service' for details.
Initially, I got this error. Then tried with su and got this.
Failed to stop odoo.service: Unit odoo.service not loaded.
My concern is how to identify the perfect file through which I can restart the Odoo services post new module installation.

How to start/stop service from python script running in Flask and Apache server using Debian?

I'm trying to start and stop services from a python script that is running using Flask and Apache.
To get the status from memcached, for example, I'm using
os.popen('service memcached status').read() and works like a charm.
The problem is that when I try to start/stop doing something like
os.popen('service memcached stop').read() it just does nothing (I checked by the shell that the service is still running)
To summarize, I can get the status but can't start/stop and don't know why its happens.
Does anyone have any suggestion?
Thanks,
I saw the apache logs in /var/log/apache2/error.log and the problem was that I needed more privileges to execute start/stop. But when I tried to use
os.popen('sudo service memcached stop').read()
I got an error, saying that I should have typed the su password.
To solve this problem I typed in the shell:
visudo
which opened the /etc/sudoers file. And there I added the line
www-data ALL=(ALL) NOPASSWD:ALL
I understood that this means that I am giving permission to the user www-data execute sudo without password.
To quit, press Ctrl+X and then y to save.
note: www-data is the username that executes the apache.

Categories