Running a python script on Google Cloud Compute Engine
You can use Google Cloud Platform tutorials itself and is very simple to follow. Links are given below
Setting up Python
https://cloud.google.com/python/setup
Getting started
https://cloud.google.com/python/getting-started/hello-world
Please note that you don't have any free tier to run Python 3.x, Standard environment with free tier only supports Python 2.x.
Edit: As per the latest update Python 3.x is default in standard environment
Just navigate to the directory where the script is placed.
python thenameofscript.py
I finally figured this out so I'll post the same answer on my own post that worked for me here. Using Debian Stretch on my VM. I'm assuming you already uploaded your file(s) to the VM and that you are in the same directory of your script.
Make your script an executable
chmod +x myscript.py
Run the
nohup
command to execute the script in the background. The&
option ensures that the process stays alive after exiting. I've added the shebang line to my python script so there's no need to callpython
herenohup /path/to/script/myscript.py &
Logout from the shell if you want
logout
Done! Now your script is up and running. You can login back and make sure that your process is still alive by checking the output of this command:
ps -e | grep myscript.py
If anything went wrong, you can check out the nohup.out
file to see the output of your script:
cat nohup.out
There is even a simpler approach to to run code in the background in gcp and in every linux terminal: using screen linux
Create a new background terminal window:
screen -S WRITE_A_NAME_OF_YOUR_CHOIC_HERE
now you are in a background window in the terminal. Run your code:
python3 mycode.py
Exit screen with the hotkeys and the job will keep running on the background.
ctrl + A + D
You can close all windows now. If you wanna go back and see what's happening. Log again in your terminal. And tap the following.
screen -ls
This one will give you the list of the created "windows". Now find yours and tap
screen -r WRITE_NAME_OF_YOUR_WINDOW
And there you have it :D You can find more commands here