Running Shutdown Scripts

Create and run shutdown scripts that execute commands right before an instance is terminated or restarted, on a best-effort basis. This is useful if you rely on automated scripts to start up and shut down instances, allowing instances time to clean up or perform tasks, such as exporting logs, or syncing with other systems.

Shutdown scripts are especially useful for instances in a managed instance group with an autoscaler. If the autoscaler shuts down an instance in the group, the shutdown script runs before the instance stops and the shutdown script performs any actions that you define. The script runs during the limited shutdown period before the instance stops. For example, your shutdown script might copy processed data to Cloud Storage or back up any logs.

Shutdown scripts function very similarly to startup scripts. Much of the documentation for startup scripts also applies for shutdown scripts.

Before you begin

Permissions required for this task

To perform this task, you must have the following permissions.

Specifications

Limitations

There are some limitations to note when using shutdown scripts:

  • For Linux instances, you can only use shutdown scripts with image version v20141007 or later. For Windows instances, you can only use shutdown scripts with image v20150310 or later.
  • Compute Engine only executes shutdown scripts on a best-effort basis and does not guarantee that the shutdown script will be run in all cases.

Shutdown script invocation

Shutdown scripts execute when an instance is scheduled to restart or terminate. There are many ways to restart or terminate an instance, but only some actions trigger the shutdown script to run. A shutdown script runs as part of the following actions:

  • When an instance shuts down due to an instances.delete request or an instances.stop request to the API.
  • When Compute Engine stops a preemptible instance as part of the preemption process.
  • When an instance shuts down through a request to the guest operating system, such as sudo shutdown or sudo reboot.
  • When you shut down an instance manually through the Cloud Platform Console or the gcloud compute tool.

The shutdown script will not run if the instance is reset using instances().reset.

A shutdown script can be of any file type. If there is a shutdown script present, Compute Engine will:

  1. Copy the script to a local file in the instance.
  2. Set permissions on the file to make it executable.
  3. Execute the file when the instance is terminated.

You could, for example, provide a Python script instead of a bash script. Keep in mind that Compute Engine runs the script verbatim, regardless of the type of script.

To execute a script that is not bash, add a shebang line at the top of the file to let the operating system know which intepreter to use. For example, for a Python script, you can add a shebang line like:

#!/usr/bin/python

Shutdown script running time

Before an instance shuts down or restarts, the shutdown script has a limited time period to run. During this period, Compute Engine attempts to run your shutdown script. If the script takes longer than this time period to complete, the instance automatically terminates and all running tasks are killed. If you shut down or restart an instance by making a request to the guest operating system with the sudo shutdown command, the limit does not apply.

The length of the shutdown period is different depending on type of the instance. Preemptible instances have a shorter shutdown period than normal instances. For more information on the shutdown time limits on each instance type, see Shutdown period.

In general, your shutdown script must finish running within its shutdown period so that the operating system has time to complete its shutdown process and flush buffers to disk.

Use a local shutdown script

A local shutdown script is a script that lives on your local computer. Pass in a local shutdown script either as a file or by giving the contents directly to Compute Engine.

Shutdown scripts can perform as many actions as you need, but if you are passing in the file locally, your script cannot exceed the metadata value length limit of 256 KB. To use a script that exceeds the length limit, store your file on Google Cloud Storage. See Use shutdown script from Google Cloud Storage for more information.

Provide a shutdown script file

You can only pass in a local shutdown script file through the gcloud command-line tool.

To pass in a local shutdown script file, supply the --metadata-from-file flag, followed by a metadata key pair, shutdown-script=PATH/TO/FILE, where PATH/TO/FILE is a relative path to the shutdown script. For example:

gcloud compute instances create example-instance \
    --metadata-from-file shutdown-script=examples/scripts/install.sh

Provide shutdown script contents directly

Alternatively, you can pass in the contents of your shutdown script directly.

Console

In Cloud Platform Console, specify a shutdown script directly using the shutdown-script metadata key:

  1. In the Cloud Platform Console, go to the VM Instances page.

    Go to the VM Instances page

  2. Click the Create instance button.
  3. On the Create a new instance page, fill in the desired properties for your instance. For advanced configuration options, expand the Management, disk, networking, SSH keys section.
  4. In the Metadata section, fill in shutdown-script as the metadata key.
  5. In the Value box, supply the contents of your shutdown script.
  6. Click the Create button to create the instance.

gcloud

Using the gcloud command-line tool, use the --metadata flag to provide the contents of your shutdown script, followed by the shutdown-script=CONTENTS key pair, where CONTENTS is the content of your shutdown script.

gcloud compute instances create example-instance --metadata shutdown-script="#! /bin/bash
> # Shuts down Apache server
> /etc/init.d/apache2 stop"

API

In the API, provide a shutdown script as part of the metadata property in your request when you create an instance. Use shutdown-script as the metadata key:

POST https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances

{...
  "metadata": {
    "items": [
      {
       "key": "shutdown-script",
       "value": "#! /bin/bash\n\n# Shuts down Apache server\n/etc/init.d/apache2 stop"
      }
    ]
  }...
}

Provide a shutdown script on Windows instances

Run shutdown scripts on Windows instances using the following Windows-specific metadata keys. Choose from any of the specialized keys listed below. Each key should match the type of script you want to run.

You can specify multiple shutdown scripts by passing in different keys to your instance but each key can only be specified once per virtual machine.

The following keys can be used with a local shutdown script, using the same instructions above.

cmd shutdown scripts bat shutdown scripts ps1 shutdown scripts
windows-shutdown-script-cmd windows-shutdown-script-bat windows-shutdown-script-ps1

Use a shutdown script from Google Cloud Storage

You can store and use a shutdown script from Google Cloud Storage. Follow the instructions in the Startup scripts documentation but replace startup-script-url with shutdown-script-url.

For Windows instances, replace windows-startup-script-url with windows-shutdown-script-url.

Apply a shutdown script to running instances

To add a shutdown script to a running instance, follow the instructions in the Applying a startup script to running instances documentation but replace the metadata keys with one of the following keys:

  • shutdown-script: Supply the shutdown script contents directly with this key. Using the gcloud command-line tool, you can provide the path to a shutdown script file, using the --metadata-from-file flag and the shutdown-script metadata key.
  • shutdown-script-url: Supply a Google Cloud Storage URL to the shutdown script file with this key.

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.

Send feedback about...

Compute Engine Documentation