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.