Search code examples
pythonubuntugoogle-cloud-platformhttpserver

server program exits when i close SSH connection to GCP


I have an Ubuntu instance on Google Cloud Platform (GCP). I want to use it as an HTTP server to access files. I simply use this python command, type it in bash:

python3 -m http.server 8000

This will run http.server module as a script, construct a simple HTTP server and listen at port 8000.

Problem is that, since I use GCP instance, I must connect to it remotely (for example I use SSH shell provided by GCP). When I close the SSH shell, the python HTTP server will stop. So what should I do to make sure that the server still runs after I close the shell?

I did searched on Google, and I tried to use

nohup python3 -m http.server 8000 &

This command, I quote, will run the instruction as a background program and persist running after exiting bash. But it seems that this doesn't work for my situation.

Anybody can help?


Solution

  • Try the screen command. I think it's easier to use and also more flexible than nohup as you can also reattach processes after detaching then. See this answer for details.