Question

Flask app mysteriously shuts off...

Hello. I created a flask app in a digital ocean droplet. I ran the Python script and made sure it was working, then I closed the terminal. The script should run forever, but I woke up this morning to complaints from a customer saying the flask app is down. I logged into my droplet and ran the script again and everything is working now, but I am worried that this will happen again. Is there a maximum execution time for Python scripts on a droplet? How can I make sure my script is always running?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
December 18, 2023

Heya @tylersquid,

How are you running your Flask app? Maybe you should look into a process manager like pm2 which restarts your Program on failure or Gunicorn.

It’s possible your Droplet killed your application due to a lack of resources as well.

Here is the article on how to configure Gunicorn with your App:

https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-22-04

Alternatively, as mentioned you can use pm2.

Install PM2: If you haven’t installed PM2 yet, you can do so using npm (Node Package Manager). Make sure you have Node.js installed first.

npm install pm2 -g

Start Your Flask App with PM2: Use PM2 to start and manage your Flask app:

pm2 start "Command you run to start your Flask APp" --name "your-app-name"

Replace "your-app-name" with a suitable name for your application. Replace ""Command you run to start your Flask APp" with a the command you use to run the application.

Save the PM2 Process List: Save the current process list to ensure PM2 restarts your app after a system reboot:

pm2 save

Monitor and Manage: You can monitor your app’s logs, restart it, or stop it using PM2 commands:

  • To view logs: pm2 logs
  • To restart: pm2 restart your-app-name
  • To stop: pm2 stop your-app-name

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel