Flask app inside VM is getting interrupted

Prottay Adhikary 20 Reputation points
2024-03-13T22:19:27.1366667+00:00

I have created an flask app and trying to host it inside a VM to make it public for testing. But the app is getting interrupted after some time. Basically the Azure CLI is getting off as well as the flask app. How to solve this?

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,132 questions
Azure
Azure
A cloud computing platform and infrastructure for building, deploying and managing applications and services through a worldwide network of Microsoft-managed datacenters.
955 questions
0 comments No comments
{count} votes

Accepted answer
  1. Anveshreddy Nimmala 2,305 Reputation points Microsoft Vendor
    2024-03-14T06:45:18.77+00:00

    Hello Prottay Adhikary,

    Welcome to microsoft Q&A, Thankyou for posting your query here.

    Hosting a Flask app inside a VM (Virtual Machine) and ensuring it remains running continuously involves a few steps and considerations.

    Running your Flask app as a service using systemd is a more robust solution.

    This ensures that your app starts automatically after a reboot and can be managed easily with service commands.

    Create a service file for your Flask app using cmd "sudo nano /etc/systemd/system/flaskapp.service"

    Add the following configuration

    Reload the systemd manager configuration, start the Flask app service, and enable it to start on boot.

    sudo systemctl daemon-reload
    sudo systemctl start flaskapp
    sudo systemctl enable flaskapp
    

    To address the issue of Azure CLI or SSH sessions timing out, you can use two different ways, using nohup to run commands without hanging up and adjusting the ServerAliveInterval in your SSH configuration.

    To use nohup with your Flask application.

    Navigate to your Flask application directory.

    Run your Flask application with nohup "nohup flask run --host=0.0.0.0 > flaskapp.log 2>&1 &"

    Here, "> flaskapp.log 2>&1 &" redirects both standard output (stdout) and standard error (stderr) to a file named flaskapp.log, so you can check the output later, and the final & runs the process in the background.

    The ServerAliveInterval SSH parameter can prevent timeouts by sending a null packet to the server at regular intervals to keep the connection alive.

    Open your SSH configuration file in an editor. This file is usually located at ~/.ssh/config on your client machine. if don't exist please you create it.

    Add or modify the configuration for the host you are connecting to. If you want this setting to apply to all SSH connections, you can use * as the host.

    "Host *
        ServerAliveInterval 60
    "
    

    Save the file and the next time you SSH into your server, this setting will be used, helping to keep the connection alive even during periods of inactivity.

    If an answer has been helpful, please consider accepting the answer to help increase visibility of this question for other members of the Microsoft Q&A community. If not, please let us know what is still needed in the comments so the question can be answered. Thank you for helping to improve Microsoft Q&A!image

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful