Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically exit/stop the running instance

I have managed to create an instance and ssh into it. However, I have couple of questions regarding the Google Compute Engine.

  1. I understand that I will be charged for the time my instance is running. That is till I exit out of the instance. Is my understanding correct?
  2. I wish to run some batch job (java program) on my instance. How do I make my instance stop automatically after the job is complete (so that I don't get charged for the additional time it may run)
  3. If I start the job and disconnect my PC, will the job continue to run on the instance?

Regards, Asim

like image 586
AAP Avatar asked May 17 '13 12:05

AAP


People also ask

How do I stop a VM instance?

To stop a VM, use the Google Cloud console, the gcloud CLI, or the Compute Engine API. In the Google Cloud console, go to the VM instances page. Select one or more VMs that you want to stop. Click Stop.

Does GCP charge for stopped instances?

A stopped instance does not incur charges, but all of the resources that are attached to the instance will still be charged. For example, you are charged for persistent disks and external IP addresses according to the price sheet, even if an instance is stopped.


3 Answers

Correct, instances are charged for the time they are running. (to the minute, minimum 10 minutes). Instances run from the time they are started via the API until they are stopped via the API. It doesn't matter if any user is logged in via SSH or not. For most automated use cases users never log in - programs are installed and started via start up scripts.

You can view your running instances via the Cloud Console, to confirm if any are currently running.

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

For example, to start your instance from the command line with the compute-rw scope:

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

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

Later, after your batch job completes, you can remove the instance from inside the instance:

$ gcutil deleteinstance -f <instance name>
like image 110
Brian Dorsey Avatar answered Oct 18 '22 01:10

Brian Dorsey


You can put halt command at the end of your batch script (assuming that you output your results on persistent disk). After halt the instance will have a state of TERMINATED and you will not be charged. See https://developers.google.com/compute/docs/pricing scroll downn to "instance uptime"

like image 6
Yuri Ginsburg Avatar answered Oct 18 '22 03:10

Yuri Ginsburg


You can auto shutdown instance after model training. Just run few extra lines of code after the model training is complete.

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 the training is complete GCP instance automatically shuts down. More info on official website: https://cloud.google.com/compute/docs/reference/rest/v1/instances/stop

like image 2
Karthik Bharath Avatar answered Oct 18 '22 01:10

Karthik Bharath