创建并运行可在虚拟机实例停止或重启前执行命令的关停脚本。如果您依靠自动脚本启动和关停实例,这将非常有用,因为实例有时间清理或执行任务,如导出日志或与其他系统同步。
关停脚本对于具有自动扩缩器的托管实例组中的虚拟机尤其有用。如果自动扩缩器关停组中的虚拟机,则关停脚本会在虚拟机停止前运行并执行您定义的任何操作。该脚本在虚拟机停止前的有限关停期运行。例如,关停脚本可能会将已处理的数据复制到 Cloud Storage 或备份任何日志。
关闭脚本的运行方式与启动脚本非常相似。启动脚本的大部分文档也适用于关停脚本。
对于关停和重新启动任务,虚拟机始终按以下方式运行关闭脚本:
- 对于 Linux 虚拟机,请使用
root
用户。 - 对于 Windows 虚拟机,请使用
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
-
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.
- Set a default region and zone.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
- 创建新实例所需的所有权限
- 针对实例的
compute.instances.setMetadata
权限 - Compute Engine 仅保证尽量执行关停脚本。在极少数情况下,Compute Engine 无法保证关停脚本一定会完成。
- 在 Windows 上,本地组策略用于启动关停脚本。
- 安装软件包会配置本地组策略
Computer Configuration/Windows Setting/Scripts (Startup/Shutdown)
设置,以在系统关闭时启动脚本。
- 安装软件包会配置本地组策略
您执行以下操作:
Compute Engine 在经过编程的停止或删除操作中停止实例。
Compute Engine 在抢占过程中停止 Spot 虚拟机或抢占式实例。
- 将脚本复制到实例中的本地文件。
- 设置针对该文件的权限,以使脚本可执行。
- 在实例关停时执行该文件。
转到创建实例页面。
指定虚拟机详情。
展开高级选项部分。
展开管理,然后执行以下操作:
- 在元数据部分,点击添加项。
- 在键字段中,输入
shutdown-script
作为元数据键。 - 在值字段中,添加关停脚本的内容。
继续虚拟机创建过程。
shutdown-script
:使用此键直接提供关停脚本内容。借助 Google Cloud CLI,您可以使用--metadata-from-file
标志和shutdown-script
元数据键提供关停脚本文件的路径。shutdown-script-url
:使用此键提供关停脚本文件的 Cloud Storage 网址。连接到实例并运行以下命令。
sudo journalctl -u google-shutdown-scripts.service
在 Google Cloud 控制台中通过串行端口 1 查看输出,并检查是否有
google_metadata_script_runner
事件。Windows 工作站上的 IAP 桌面。如需了解详情,请参阅 GitHub 上的 GoogleCloudPlatform/iap-desktop 仓库。
Google Cloud 控制台中的串行端口 1。如需了解详情,请参阅查看串行端口输出。
Windows 事件查看器的应用日志。
Windows 工作站上的 IAP 桌面。如需了解详情,请参阅 GitHub 上的 GoogleCloudPlatform/iap-desktop 仓库。
Terraform
如需在本地开发环境中使用本页面上的 Terraform 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。
如需了解详情,请参阅 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 Engine 将执行以下操作:
例如,您可以提供 Python 脚本而不是 bash 脚本。请注意,无论是什么类型的脚本,Compute Engine 都会逐字运行脚本。
要执行非 bash 脚本,请在文件顶部添加 shebang 行,告知操作系统要使用哪个解释器。例如,对于 Python 脚本,您可以添加如下的 shebang 行:
#!/usr/bin/python
关停脚本运行时间
当 Compute Engine 将实例状态设置为
STOPPING
时,关停脚本会开始运行。您的脚本必须在实例完全停止(此时其状态会变为TERMINATED
)之前完成。实例达到TERMINATED
状态所需的时间因实例类型而异。如果脚本运行时间超过实例停止所需的时间,Compute Engine 会强制停止脚本,这可能会导致数据丢失或任务未完成。为了防止出现这种情况,请从客机操作系统内部停止实例。此方法会将实例状态保持为
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
资源与元数据中的关停脚本的路径结合使用。直接提供关停脚本内容
或者,您可以直接传递关停脚本的内容。
控制台
在 Google Cloud 控制台中,直接使用
shutdown-script
元数据键指定关停脚本: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
资源与元数据中的关停脚本结合使用。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
。将关停脚本应用于正在运行的实例
要将关停脚本添加到正在运行的实例,请按照将启动脚本应用于正在运行的实例文档中的说明操作,但使用以下键之一替换元数据键:
查看关停脚本的输出
Linux
您可以通过执行以下任一操作来查看 Linux 关停脚本的输出:
Windows
使用以下任一项并检查是否有
GCEMetadataScripts
事件,查看 Windows Server 关停脚本的输出:如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-06-25。
-