Announcements
This site is in read only until July 22 as we migrate to a new platform; refer to this community post for more details.
Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Start VM > Execute Python script > Stop VM

H123
New Member

I currently have a VM Instance (Linux Debian) within google cloud that has a functioning environment for my python project, and everything works when inputted: "python3 run.py" via SSH command line.

How can I automate the running of the instance > then the execution of that python file > then the stopping of the instance? I couldn't find very much when looking around

1 2 1,937
2 REPLIES 2

Look at the starting and stopping of an instance! https://cloud.google.com/compute/docs/instances/schedule-instance-start-stop

Clouds

 

If the task you are running can take a variable amount of time (and so it would be difficult to schedule it to turn off), you could start it on the schedule and then the script itself could initiate the shutdown by making a "gcloud" call directly from the VM itself once it has completed whatever processing it is carrying out.

One other thing to add, if the script takes less than 1 hour, you might want to keep an eye on Cloud Run Jobs, this is a new service in preview, but could be useful depending on what you are trying to do, see: https://cloud.google.com/run/docs/create-jobs