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

Amazon Systems Manager Change Manager 不再向新客户开放。现有客户可以继续正常使用该服务。有关更多信息,请参阅 Amazon Systems Manager Change Manager 可用性变更

运行命令时处理重启问题

如果使用 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 2
#!/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 }