运行命令时处理重启问题 - Amazon Systems Manager
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

运行命令时处理重启问题

如果您使用Run Command功能来运行重启托管节点的 Amazon Systems Manager脚本,我们建议您在脚本中指定退出代码。如果您使用其他一些机制尝试通过脚本重启节点,则即使重启是脚本的最后一步,脚本执行状态也可能无法正确更新。对于 Windows 托管式节点,您需在脚本中指定 exit 3010。对于 Linux 和 macOS 托管式节点,需要指定 exit 194。退出代码指示 Amazon Systems Manager Agent (SSM Agent) 重新启动托管节点,然后在重新启动完成后重新启动脚本。在重启开始之前,SSM Agent 会通知云中的 Systems Manager 服务,通信将在服务器重启期间中断。

注意

重启脚本不能作为 aws:runDocument 插件的一部分。如果一个文档包含重启脚本,另一个文档尝试通过 aws:runDocument 插件运行该文档,则 SSM Agent 会返回错误。

创建幂等脚本

在开发用于重启托管式节点的脚本时,使脚本具有幂等性,以便脚本执行在重启后从中断的位置继续进行。幂等脚本管理状态并验证是否执行了该操作。当一个步骤设定为仅运行一次时,可以防止该步骤多次运行。

以下是多次重启托管式节点的幂等脚本的概要示例。

$name = Get current computer name If ($name –ne $desiredName) { Rename computer exit 3010 } $domain = Get current domain name If ($domain –ne $desiredDomain) { Join domain exit 3010 } If (desired package not installed) { Install package exit 3010 }

示例

以下脚本示例使用退出代码来重新启动托管式节点。Linux 示例在 Amazon Linux 上安装软件包更新,然后重新启动该节点。该Windows Server示例在节点上安装 Telnet-Client,然后将其重新启动。

Amazon Linux
#!/bin/bash yum -y update needs-restarting -r if [ $? -eq 1 ] then exit 194 else exit 0 fi
Windows
$telnet = Get-WindowsFeature -Name Telnet-Client if (-not $telnet.Installed) { # Install Telnet and then send a reboot request to SSM Agent. Install-WindowsFeature -Name "Telnet-Client" exit 3010 }