執行關機指令碼


建立和執行關閉指令碼,使其在虛擬機器 (VM) 執行個體停止或重新啟動前,執行指令。如果您依賴自動指令碼來啟動和關閉執行個體、讓執行個體清理或執行任務 (例如匯出記錄或與其他系統同步),這功能會非常實用。

代管執行個體群組中具有自動配置器的 VM 來說,關機指令碼是一項特別實用的功能。假如自動配置器關閉群組中的 VM,則關機指令碼會在 VM 停止之前開始執行,而且會執行您定義的任何操作。指令碼會在 VM 停止前的有限關閉期限中執行。舉例來說,您的關機指令碼可能會將已處理的資料複製到 Cloud Storage 或備份任何記錄檔。

關機指令碼的功能與開機指令碼非常相似。開機指令碼的多數說明文件也適用於關機指令碼。

無論是關機還是重新啟動工作,VM 都會執行以下關機指令碼:

  • 如為 Linux VM,請使用 root 使用者。
  • 針對 Windows VM,請使用 System 帳戶。

事前準備

  • 瞭解開機指令碼
  • 瞭解何謂中繼資料伺服器
  • 如果尚未設定,請先設定驗證機制。「驗證」是指驗證身分,以便存取 Google Cloud 服務和 API 的程序。如要從本機開發環境執行程式碼或範例,請選取下列任一選項,以便對 Compute Engine 進行驗證:

    Select the tab for how you plan to use the samples on this page:

    Console

    When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.

    gcloud

    1. After installing the Google Cloud CLI, initialize it by running the following command:

      gcloud init

      If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    2. Set a default region and zone.
    3. Terraform

      如要在本機開發環境中使用本頁面的 Terraform 範例,請先安裝並初始化 gcloud CLI,然後使用您的使用者憑證設定應用程式預設憑證。

      1. Install the Google Cloud CLI.

      2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      3. To initialize the gcloud CLI, run the following command:

        gcloud init
      4. If you're using a local shell, then create local authentication credentials for your user account:

        gcloud auth application-default login

        You don't need to do this if you're using Cloud Shell.

        If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

      5. 詳情請參閱 Set up authentication for a local development environment

      REST

      如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。

        After installing the Google Cloud CLI, initialize it by running the following command:

        gcloud init

        If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      詳情請參閱 Google Cloud 驗證說明文件中的「驗證以使用 REST」。

這項工作需要的權限

如要執行這項工作,您必須具備以下權限

  • 建立執行個體需要的所有權限
  • 執行個體的 compute.instances.setMetadata 權限

規格

以下各節將概略說明關機指令碼的規格。

限制

使用關機指令碼時,一些必須注意的限制如下:

  • Compute Engine 會儘可能執行關機指令碼,但不保證能在所有情況下都完全執行關機指令碼 (有極少數情況無法完成)。
  • 在 Windows 上,系統會使用本機群組政策啟動關機指令碼。
    • 安裝套件會設定本機群組政策 Computer Configuration/Windows Setting/Scripts (Startup/Shutdown) 設定,以便在系統關機時啟動指令碼。

關機指令碼叫用

執行個體在發生下列任一事件後關閉時,系統會觸發關閉指令碼:

  • 請執行下列操作:

  • Compute Engine 會在預先設定的停止或刪除作業中停止執行個體。

  • 屬於先佔程序的一部分,由 Compute Engine 停止 Spot VM 或先占執行個體。

關機指令碼可以是任何檔案類型。如果執行個體中有關機指令碼,Compute Engine 會執行以下操作:

  1. 將指令碼複製到執行個體中的本機檔案。
  2. 設定檔案的權限,讓指令碼可供執行。
  3. 在執行個體關閉時執行該檔案。

例如,您可以提供 Python 指令碼而不是 Bash 指令碼。請注意,Compute Engine 會確實地執行指令碼,無論其類型為何。

如要執行非 Bash 指令碼,請在檔案頂端新增一個工作行,讓作業系統知道要使用哪個解譯器。舉例來說,您可以針對 Python 指令碼新增如下的工作行:

#!/usr/bin/python

關機指令碼執行時間

當 Compute Engine 將執行個體狀態設為 STOPPING 時,關機指令碼就會開始執行。指令碼必須在執行個體完全停止前完成,也就是其狀態變更為 TERMINATED 時。執行個體達到 TERMINATED 狀態所需的時間,會因執行個體類型而異。

如果指令碼的執行時間超過執行個體的停止時間,Compute Engine 就會強制停止指令碼,這可能導致資料遺失或工作未完成。為避免這種情況,請在客體 OS 中停止執行個體。此方法會將執行個體狀態維持為 STOPPING,直到關機指令碼完成執行為止。

使用本機關機指令碼

本機關機指令碼是存放在本機電腦的指令碼。您可以將本機關機指令碼透過檔案的形式傳入,或是直接將內容傳給 Compute Engine。

關機指令碼可以根據需要執行不限次數的操作,但如果您在本機傳入檔案,則指令碼長度不得超過中繼資料值的上限 256 KB。如要使用長度超過上限的指令碼,請將檔案儲存至 Cloud Storage。詳情請參閱透過 Cloud Storage 使用關機指令碼一節。

提供關機指令碼檔案

您只能透過 gcloud 指令列工具傳入本機關機指令碼檔案。

gcloud

如要傳入本機關機指令碼檔案,請加入 --metadata-from-file 旗標並在後面加上中繼資料鍵值組合 shutdown-script=PATH/TO/FILE,其中 PATH/TO/FILE 是關機指令碼的相對路徑。例如:

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

Terraform

如要直接指定關機指令碼,請使用 google_compute_instance 資源,並在中繼資料中提供關機指令碼的路徑。

resource "google_compute_instance" "shutdown_content_from_file" {
  name         = "instance-name-shutdown-content-from-file"
  machine_type = "f1-micro"
  zone         = "us-central1-c"
  metadata = {
    # Shuts down Apache server
    shutdown-script = file("${path.module}/shutdown-script.sh")
  }
  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-11"
    }
  }
  network_interface {
    # A default network is created for all Google Cloud projects
    network = "default"
    access_config {
    }
  }
}

直接提供關機指令碼

您也可以直接傳入關機指令碼內容。

主控台

在 Google Cloud 主控台中,使用 shutdown-script 中繼資料鍵值直接指定關機指令碼:

  1. 前往「Create an instance」(建立執行個體) 頁面。

    前往「Create an instance」

  2. 指定 VM 詳細資料。

  3. 展開「Advanced options」(進階選項) 區段。

  4. 展開「管理」,然後執行下列操作:

    1. 在「中繼資料」專區中,按一下「新增項目」
    2. 在「Key」欄位中,輸入中繼資料鍵 shutdown-script
    3. 在「Value」欄位中,新增關機指令碼內容。
  5. 繼續執行 VM 建立程序。

gcloud

使用 Google Cloud CLI 時,請使用 --metadata 標記提供關機指令碼的內容,後面加上 shutdown-script=CONTENTS 鍵值組合,其中 CONTENTS 是關機指令碼的內容。

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

Terraform

如要直接指定關機指令碼,請在中繼資料中使用 google_compute_instance 資源搭配關機指令碼。

resource "google_compute_instance" "default" {
  name         = "instance-name-shutdown-content-directly"
  machine_type = "f1-micro"
  zone         = "us-central1-c"
  metadata = {
    # Shuts down Apache server
    shutdown-script = "#! /bin/bash /etc/init.d/apache2 stop"
  }
  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-11"
    }
  }
  network_interface {
    # A default network is created for all Google Cloud projects
    network = "default"
    access_config {
    }
  }
}

REST

在 API 中,建立執行個體時,請在要求中提供做為中繼資料屬性的關機指令碼。使用 shutdown-script 做為中繼資料鍵值:

POST https://compute.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"
      }
    ]
  }...
}

在 Windows 執行個體上提供關機指令碼

使用以下 Windows 專用中繼資料鍵值,在 Windows 執行個體上執行關機指令碼。可從下面列出的所有特殊鍵中加以選擇。每組鍵值都應與您要執行的指令碼類型相符。

您可以透過向執行個體傳入不同的鍵值來指定多個關機指令碼,但每個虛擬機器只能指定一組鍵值。

以下鍵值可與關機指令碼一起使用,請參閱上述的相同操作說明

cmd 關機指令碼 bat 關機指令碼 ps1 關機指令碼
windows-shutdown-script-cmd windows-shutdown-script-bat windows-shutdown-script-ps1

透過 Cloud Storage 使用關機指令碼

您可以透過 Cloud Storage 儲存和使用關機指令碼。請按照開機指令碼說明文件中的指示操作,但要將 startup-script-url 替換成 shutdown-script-url

如果是 Windows 執行個體,請將 windows-startup-script-url 替換成 windows-shutdown-script-url

為執行中的執行個體套用關機指令碼

如要將關機指令碼新增至執行中的執行個體,請按照將開機指令碼套用至執行中的執行個體說明文件的指示操作,但是要將中繼資料鍵換成下列鍵值:

  • shutdown-script:直接使用這個鍵值提供關機指令碼內容。您可以使用 Google Cloud CLI,透過 --metadata-from-file 標記和 shutdown-script 中繼資料鍵值,提供關機指令碼檔案的路徑。
  • shutdown-script-url:使用這個鍵值將 Cloud Storage URL 提供給關機指令碼檔案。

查看關機指令碼的輸出內容

Linux

您可以執行下列任一操作,查看 Linux 關機指令碼的輸出內容:

Windows

使用下列任一方法並檢查 GCEMetadataScripts 事件,即可查看 Windows Server 關機指令碼的輸出內容: