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.

  1. Make your script an executable

    chmod +x myscript.py
    
  2. 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 call python here

    nohup /path/to/script/myscript.py &
    
  3. 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
Answer from EsotericVoid on Stack Overflow
🌐
Google Cloud
cloud.google.com › python › getting started with python on compute engine
Getting started with Python on Compute Engine | Google Cloud Documentation
From Cloud Shell, you can deploy to a single Compute Engine instance virtual machine (VM) which runs your app. You need a way to instruct your instance to download and run your code.
🌐
Google
codelabs.developers.google.com › codelabs › cloud-run-hello-python3
Hello Cloud Run with Python | Google Codelabs
March 20, 2026 - In the Cloud Shell window, click the Web Preview icon and select Preview on port 8080: This should open a browser window showing the Hello World! message. You can also open another Cloud Shell session (a new terminal tab) by clicking the + icon and sending a web request to the application running ...
Discussions

How can I run cloud shell commands from Python? - Stack Overflow
I've been trying to automate my GCP dataflow system. Uncompressed txt files are loaded into the pipeline much faster as compared to compressed gzip files because of some parallelization issue. So, ... More on stackoverflow.com
🌐 stackoverflow.com
Which GCP service/process to use to run a python script that generates data and writes it to a storage bucket
If you are just trying to trigger a basic function as described: Cloud functions. Cloud run is more for continuously running code that needs to be scaled based on demands. More on reddit.com
🌐 r/dataengineering
18
7
December 6, 2023
virtual machine - running a python script on cloud shell and saving ouput into cloud sql - Stack Overflow
I want to run a python script which takes input around 350 images and the output which is generated has more than 700 million rows so it takes a lot of time to process. That's why i wanted to run the script on google cloud shell and save the output in the form of cloud sql database. More on stackoverflow.com
🌐 stackoverflow.com
What's the best way to run python scripts on the cloud to process Google Sheets data?
There’s probably 100 ways to do this, but I’ve done something similar using google cloud platform services. I’d use cloud functions to run the script. Use gspread to interact with your sheet. Not sure what the best option is to save the output to google drive, but there are some google client libraries that can probably assist with that. You’ll need to sort out Auth and permissions, which can be a pain but is doable. You can trigger the cloud function on a schedule, or manually, or whatever other triggering mechanism you want to use. Depending on what you are doing, you’d probably stay in the free tier. More on reddit.com
🌐 r/pythontips
9
3
March 16, 2024
Top answer
1 of 5
39

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.

  1. Make your script an executable

    chmod +x myscript.py
    
  2. 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 call python here

    nohup /path/to/script/myscript.py &
    
  3. 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
2 of 5
9

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

🌐
GitHub
github.com › GoogleCloudPlatform › cloud-shell-tutorials › blob › master › cloud-console-tutorials › python_gae_quickstart › python_gae_quickstart.md
cloud-shell-tutorials/cloud-console-tutorials/python_gae_quickstart/python_gae_quickstart.md at master · GoogleCloudPlatform/cloud-shell-tutorials
This tutorial shows you how to deploy a sample Python application to Google App Engine using the gcloud command. Here are the steps you will be taking. ... Projects bundle code, VMs, and other resources together for easier development and monitoring. ... You will learn how to run your app using Google Cloud Shell, right in your browser.
Author   GoogleCloudPlatform
🌐
Google Cloud
cloud.google.com › python › client libraries › python client for cloud shell
Python Client for Cloud Shell | Python client libraries | Google Cloud Documentation
python3 -m venv <your-env> source <your-env>/bin/activate pip install google-cloud-shell · py -m venv <your-env> .\<your-env>\Scripts\activate pip install google-cloud-shell
🌐
C2cglobal
c2cglobal.com › articles › how-to-run-python-on-google-cloud-1356
How to Run Python on Google Cloud
Optimize Python for Google Cloud with these essential steps. Set up, migrate, and deploy effortlessly for efficient cloud computing.
🌐
DEV Community
dev.to › njoguu › running-a-python-script-on-google-cloud-in-2023-a-step-by-step-guide-272k
Running a Python Script on Google Cloud in 2023: A Step-by-Step Guide - DEV Community
May 31, 2023 - A Google Cloud Platform account: Sign up for GCP if you don't already have an account. Python installed on your local machine: Ensure you have Python installed, preferably version 3.x. A basic understanding of the command line interface (CLI). A basic understanding of Git. Make sure you have a repository set up that contains the Python scripts you want to run on Google Cloud.
Find elsewhere
🌐
O'Reilly
oreilly.com › library › view › hands-on-machine-learning › 9781788393485 › 6385da47-a68b-44c4-88ef-b73b0d1f66af.xhtml
Run Python code on Google Cloud Shell - Hands-On Machine Learning on Google Cloud Platform [Book]
Run Python code on Google Cloud Shell Google Cloud Shell provides command-line access to cloud resources directly from your browser. You can easily manage projects and resources... - Selection from Hands-On Machine Learning on Google Cloud Platform [Book]
🌐
YouTube
youtube.com › watch
Using Python on Google Cloud with Cloud Run - YouTube
Check out our lab → https://goo.gle/3m9K7W3 Welcome to the first episode of Serverless Expeditions! In this series, we’ll explore everything having to do wi...
Published   September 17, 2020
🌐
Reddit
reddit.com › r/dataengineering › which gcp service/process to use to run a python script that generates data and writes it to a storage bucket
r/dataengineering on Reddit: Which GCP service/process to use to run a python script that generates data and writes it to a storage bucket
December 6, 2023 -

This is just me messing about - but I'm wondering what the 'best' (or most common) approach to this might be ? Basically I have a python module that will generate some fake data - and I want to write that to a storage bucket on a cloud scheduled job. So every X minutes it'll generate some synthetic data into some bucket (assume <=1GB of data).

It seems that cloud run / cloud functions are the most obvious for this task, but which one and why (or is there something else more suited to running python code like this?)

Reading some of the documentation for cloud run https://cloud.google.com/run/docs/developing it seems to be geared towards web services (most of the examples seem to use flask...), and I'm not sure that's what I'm after here.

I figured it'd be a fairly easy question for people with GCP experience.

Thanks

🌐
Anderfernandez
anderfernandez.com › en › blog › automate-python-script-google-cloud
How to automate a Python script on Google Cloud - Ander Fernández
In this post I explain step by step how you can automate a Python script in Google Cloud with Cloud Functions, Cloud Scheduler and Pub/Sub.
🌐
Medium
victorbnnt.medium.com › schedule-python-scripts-on-google-cloud-platform-from-scratch-ab6820f094af
Schedule Python scripts on Google Cloud Platform from scratch | by Victor Bonnet | Medium
December 4, 2021 - Startup scripts are executed at the root of the VM, so we want to configure the environment at the root of the machine. On the terminal, enter the following command and click enter: ... Now we are at root and we can setup the environment. Execute the following lines line by line. sudo apt update sudo apt install python3 python3-dev python3-venv sudo apt-get install wget wget https://bootstrap.pypa.io/get-pip.py sudo python3 get-pip.py python3 -m venv env