How to automatically exit / stop a running instance

I managed to create an instance and ssh in it. However, I have a few questions regarding the Google Compute Engine.

  • I understand that I will be charged that my copy is working. This is until I get out of this instance. Do I understand correctly?
  • I want to run some batch job (java program) on my instance. How to make my instance automatically stop after the task is completed (so that I do not charge for the extra time that it can complete).
  • If I run the task and turn off my computer, will the work work in the instance?

Regards, Asim

+7
google-compute-engine
source share
3 answers

That's right, instances are charged during their launch. (up to a minute, at least 10 minutes). Instances run from the moment they are launched through the API until they are stopped through the API. It does not matter if any user is logged in via SSH or not. For most cases of automatic use, users never enter the system — programs are installed and run through scripts .

You can view your running instances using the Cloud Console to confirm that they are currently running.

If you want to stop your instance inside the instance, the easiest way is to start the instance using compute-rw Service Scope and use gcutil.

For example, to start an instance from the command line using the calculation area-rw:

$ gcutil --project=<project-id> addinstance <instance name> --service_account_scopes=compute-rw 

(this is the default when creating an instance manually through the Cloud Console)

Later, after completing the batch job, you can remove the instance from the instance:

 $ gcutil deleteinstance -f <instance name> 
+11
source share

You can put the halt command at the end of your batch script (provided that you output your results to a permanent drive). After stopping, the instance will have a TERMINATED state and you will not be charged. See https://developers.google.com/compute/docs/pricing scroll down to "Uptime"

+4
source share

You can automatically disconnect an instance after training the model. Just run a few extra lines of code after completing the model training.

 from googleapiclient import discovery from oauth2client.client import GoogleCredentials credentials = GoogleCredentials.get_application_default() service = discovery.build('compute', 'v1', credentials=credentials) # Project ID for this request. project = 'xyz' # Project ID # The name of the zone for this request. zone = 'xyz' # Zone information # Name of the instance resource to stop. instance = 'xyz' # instance id request = service.instances().stop(project=project, zone=zone, instance=instance) response = request.execute() 

add this to your model training script. When training is complete, the GCP instance is automatically disabled. More information on the official website: https://cloud.google.com/compute/docs/reference/rest/v1/instances/stop

0
source share

All Articles