google-compute-engine

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


Solution

  • 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>