

• AWS Systems Manager CloudWatch 控制面板在 2026 年 4 月 30 日之后将不再可用。客户可以像现在一样继续使用 Amazon CloudWatch 控制台来查看、创建和管理其 Amazon CloudWatch 控制面板。有关更多信息，请参阅 [Amazon CloudWatch 控制面板文档](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Dashboards.html)。

# 步骤 3：使用 AWS CLI 向维护时段注册任务
<a name="mw-cli-tutorial-tasks"></a>

在教程的此步骤中，您将注册 AWS Systems Manager Run Command 任务，该任务将在适用于 Linux 的 Amazon Elastic Compute Cloud (Amazon EC2) 实例上运行 `df` 命令。此标准 Linux 命令的结果显示实例的磁盘文件系统上的空闲空间数量和已用空间数量。

–或者–

如果您设定的目标是适用于 Windows Server 而不是 Linux 的 Amazon EC2 实例，请将以下命令中的 **df** 替换为 **ipconfig**。此命令的输出列出了有关目标实例上适配器的 IP 地址、子网掩码和默认网关等详细信息。

当您准备好注册其他任务类型，或使用更多可用 Systems Manager Run Command 选项时，请参阅 [示例：向维护时段注册任务](mw-cli-register-tasks-examples.md)。我们在这里提供了有关全部四种任务类型的更多信息，以及它们最重要的一些选项，以帮助您针对更广泛的真实情况制定计划。

**向维护时段注册任务**

1. 在本地计算机上运行以下命令。将每个{{示例资源占位符}}替换为您自己的信息。从本地 Windows 计算机运行的版本包含转义字符 (“/”)，您需要从命令行工具运行命令。

------
#### [ Linux & macOS ]

   ```
   aws ssm register-task-with-maintenance-window \
       --window-id {{mw-0c50858d01EXAMPLE}} \
       --task-arn "AWS-RunShellScript" \
       --max-concurrency 1 --max-errors 1 \
       --priority 10 \
       --targets "Key=InstanceIds,Values={{i-0471e04240EXAMPLE}}" \
       --task-type "RUN_COMMAND" \
       --task-invocation-parameters '{"RunCommand":{"Parameters":{"commands":["df"]}}}'
   ```

------
#### [ Windows ]

   ```
   aws ssm register-task-with-maintenance-window ^
       --window-id {{mw-0c50858d01EXAMPLE}} ^
       --task-arn "AWS-RunShellScript" ^
       --max-concurrency 1 --max-errors 1 ^
       --priority 10 ^
       --targets "Key=InstanceIds,Values={{i-02573cafcfEXAMPLE}}" ^
       --task-type "RUN_COMMAND" ^
       --task-invocation-parameters={\"RunCommand\":{\"Parameters\":{\"commands\":[\"df\"]}}}
   ```

------

   系统返回类似于下文的信息：

   ```
   {
       "WindowTaskId": "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE"
   }
   ```

1. 现在，运行以下命令查看有关您创建的维护时段任务的详细信息。

------
#### [ Linux & macOS ]

   ```
   aws ssm describe-maintenance-window-tasks \
       --window-id {{mw-0c50858d01EXAMPLE}}
   ```

------
#### [ Windows ]

   ```
   aws ssm describe-maintenance-window-tasks ^
       --window-id {{mw-0c50858d01EXAMPLE}}
   ```

------

1. 系统返回类似于以下内容的信息。

   ```
   {
       "Tasks": [
           {
               "WindowId": "mw-0c50858d01EXAMPLE",
               "WindowTaskId": "4f7ca192-7e9a-40fe-9192-5cb15EXAMPLE",
               "TaskArn": "AWS-RunShellScript",
               "Type": "RUN_COMMAND",
               "Targets": [
                   {
                       "Key": "InstanceIds",
                       "Values": [
                           "i-02573cafcfEXAMPLE"
                       ]
                   }
               ],
               "TaskParameters": {},
               "Priority": 10,
               "ServiceRoleArn": "arn:aws:iam::123456789012:role/MyMaintenanceWindowServiceRole",
               "MaxConcurrency": "1",
               "MaxErrors": "1"
           }
       ]
   }
   ```

1. 请耐心等待，直到有足够的时间根据您在[步骤 1：使用 AWS CLI 创建维护时段](mw-cli-tutorial-create-mw.md) 中指定的计划运行任务。例如，如果您指定了 **--schedule "rate(5 minutes)"**，则等待五分钟。然后，运行以下命令来查看此任务所进行的任何执行的相关信息。

------
#### [ Linux & macOS ]

   ```
   aws ssm describe-maintenance-window-executions \
       --window-id {{mw-0c50858d01EXAMPLE}}
   ```

------
#### [ Windows ]

   ```
   aws ssm describe-maintenance-window-executions ^
       --window-id {{mw-0c50858d01EXAMPLE}}
   ```

------

   系统返回类似于以下内容的信息。

   ```
   {
       "WindowExecutions": [
           {
               "WindowId": "mw-0c50858d01EXAMPLE",
               "WindowExecutionId": "14bea65d-5ccc-462d-a2f3-e99c8EXAMPLE",
               "Status": "SUCCESS",
               "StartTime": 1557593493.096,
               "EndTime": 1557593498.611
           }
       ]
   }
   ```

**提示**  
任务成功运行后，您可以降低维护时段运行的速率。例如，运行以下命令以将频率减少为每周一次。将 {{mw-0c50858d01EXAMPLE}} 替换为您自己的信息。  

```
aws ssm update-maintenance-window \
    --window-id {{mw-0c50858d01EXAMPLE}} \
    --schedule "rate(7 days)"
```

```
aws ssm update-maintenance-window ^
    --window-id {{mw-0c50858d01EXAMPLE}} ^
    --schedule "rate(7 days)"
```
有关管理维护时段计划的信息，请参阅 [参考：适用于 Systems Manager 的 Cron 和 Rate 表达式](reference-cron-and-rate-expressions.md) 和 [维护时段计划和活动期间选项](maintenance-windows-schedule-options.md)。  
有关使用 AWS Command Line Interface (AWS CLI) 修改维护时段的信息，请参阅 [教程：使用 AWS CLI 更新维护时段](maintenance-windows-cli-tutorials-update.md)。

有关运行 AWS CLI 命令以查看有关维护时段任务及其执行详细信息的练习，请转到[教程：查看有关任务和使用 AWS CLI 执行任务的信息](mw-cli-tutorial-task-info.md)。

**访问教程命令输出**  
使用 AWS CLI 查看与您的维护时段任务执行相关联的 Run Command 命令的*输出*，不在本教程的讨论范围内。

不过，您可以使用 AWS CLI 查看此数据。（您还可以在 Systems Manager 控制台中或存储在 Amazon Simple Storage Service (Amazon S3) 存储桶内的日志文件中（如果您已配置维护时段将命令输出存储在其中）查看输出。） 您会看到，适用于 Linux 的 Amazon EC2 实例上 **df** 命令的输出类似于下文：

```
Filesystem 1K-blocks Used Available Use% Mounted on

devtmpfs 485716 0 485716 0% /dev

tmpfs 503624 0 503624 0% /dev/shm

tmpfs 503624 328 503296 1% /run

tmpfs 503624 0 503624 0% /sys/fs/cgroup

/dev/xvda1 8376300 1464160 6912140 18% /
```

Windows Server 的 EC2 实例上 **ipconfig** 命令的输出类似于下文：

```
Windows IP Configuration


Ethernet adapter Ethernet 2:

   Connection-specific DNS Suffix  . : example.com
   IPv4 Address. . . . . . . . . . . : 10.24.34.0/23
   Subnet Mask . . . . . . . . . . . : 255.255.255.255
   Default Gateway . . . . . . . . . : 0.0.0.0

Ethernet adapter Ethernet:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . : abc1.wa.example.net

Wireless LAN adapter Local Area Connection* 1:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Wireless LAN adapter Wi-Fi:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::100b:c234:66d6:d24f%4
   IPv4 Address. . . . . . . . . . . : 192.0.2.0
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.0.2.0

Ethernet adapter Bluetooth Network Connection:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :
```