1

I created a simple web app as described in this tutorial and hosted it on a linux VM using Google's Compute Engine.

Unfortunately when I close my SSH connection after spinning up the flask app, the external IP Address and functionality does not persist.

How do I get it to stay up as long as my VM is up and running? Is this an issue with where the app is located in my vm (ex: www/ folder vs the /user/ folder)?

Dan Kowalczyk
  • 4,103
  • 2
  • 18
  • 29
doedotdev
  • 555
  • 4
  • 17
  • Are you sure that the IP address does not persist? – Dan Kowalczyk Jan 31 '18 at 03:53
  • @DanKowalczyk yes, it closes immediately after i close my ssh session. I am going to try the 2 methods below and mark an excepted answer. Thanks for your help so far. – doedotdev Jan 31 '18 at 19:05
  • Thanks! The other answer is great if you plan to frequently turn off the server so that the app comes back online next time you turn it on. My recommendation is nice for when you're actively developing the app. – Dan Kowalczyk Feb 01 '18 at 06:25

3 Answers3

4

The answers above are both great and both work. For the next person that is relatively new to VM/Google Compute/Cloud here is exactly how I used screen. I think this is the simplest/ best solution for just learning/ getting something stood up quickly.

Once you are in your VM, first create a screen.

screen -S <screen name>

In my case, I used

screen -S one

Now your terminal will most likely clear as you are in your screen, a new process window on the same VM. This is where we can run our flask code.

Now implement your flask/ web server code/ whatever.

export FLASK_APP=main.py
flask run

Now I am able to hit my web server link that pops up. Now you can close out of this terminal/ terminate my ssh connection, the web app is still live.

Next time I ssh in I can simply use screen -ls to show my list of available screens. It will also show the status of your screens.

googleUserName@one:~$ screen -ls
There is a screen on:
        761.one (02/01/2018 06:58:56 PM)        (Detached)

Now I can reconnect to it, I just specify the screen by using -r for reconnect.

screen -r <screen name>

In my case

screen -r one

When I reconnect my webserver is still running and shows all my outputs and traffic I would expect as if I never closed it.

Now if you want to bounce back and forth between your screen and your normal ssh connection, you can detach, -d, from it. (Here you do not have to specify the screen name, 'one' in my case, because you are currently in the screen, and will detach from the current.

screen -d

Some closing remarks...

I do not know if you can create a screen within a screen.

I do not know how sustainable this solution is/ if it is recommended. But screen works fine for my use cases.

Here is the code I am running in my main.py flask application.

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

You can find it here

The tutorial I used getting started with google compute engine, set up, and web servers is here.

doedotdev
  • 555
  • 4
  • 17
  • I'm really glad screen worked to solve your problem, however, I do think it's bad etiquette to accept your own answer. You wrote a nice tutorial for screen, but this is creeping outside of the scope of your original question. See the last few paragraphs of this blog post: https://stackoverflow.blog/2008/12/01/why-cant-i-accept-my-own-answer/ – Dan Kowalczyk Feb 01 '18 at 22:52
  • @DanKowalczyk you are absolutely right. Switched it and commented. Thanks again for your help. – doedotdev Feb 02 '18 at 01:53
  • Thanks! You should also check out tmux which is very similar. It seems Google prefers tmux since it's integrated into Cloud Shell. – Dan Kowalczyk Feb 02 '18 at 06:51
2

You'll need to run the app in a background process or in screen or tmux. This is a good primer.

For more details on how to use screen, you can check this article.

Additionally, you can check this discussion on a similar concern as well.

Digil
  • 742
  • 4
  • 12
Dan Kowalczyk
  • 4,103
  • 2
  • 18
  • 29
2

It's because you're running as a user. A few minutes after closing your ssh connection, your session will close and the process will be killed. You can either run it has a startup script (https://cloud.google.com/compute/docs/startupscript#startupscriptrunninginstances) or startup cron job as root.

Peter Li
  • 410
  • 1
  • 3
  • 13