步骤 3:向维护时段注册任务 (Amazon CLI) - Amazon Systems Manager
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

步骤 3:向维护时段注册任务 (Amazon CLI)

在教程的此步骤中,您将注册 Amazon 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 选项时,请参阅 示例:向维护时段注册任务。我们在这里提供了有关全部四种任务类型的更多信息,以及它们最重要的一些选项,以帮助您针对更广泛的真实情况制定计划。

向维护时段注册任务
  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"
    }
  2. 现在,运行以下命令查看有关您创建的维护时段任务的详细信息。

    Linux & macOS
    aws ssm describe-maintenance-window-tasks \ --window-id mw-0c50858d01EXAMPLE
    Windows
    aws ssm describe-maintenance-window-tasks ^ --window-id mw-0c50858d01EXAMPLE
  3. 系统返回类似于以下内容的信息。

    {
        "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"
            }
        ]
    }
  4. 请耐心等待,直到有足够的时间根据您在步骤 1:创建维护时段 (Amazon CLI) 中指定的计划运行任务。例如,如果您指定了 --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 替换为您自己的信息。

Linux & macOS
aws ssm update-maintenance-window \ --window-id mw-0c50858d01EXAMPLE \ --schedule "rate(7 days)"
Windows
aws ssm update-maintenance-window ^ --window-id mw-0c50858d01EXAMPLE ^ --schedule "rate(7 days)"

有关管理维护时段计划的信息,请参阅 参考:适用于 Systems Manager 的 Cron 和 Rate 表达式维护时段计划和活动期间选项

有关使用 Amazon Command Line Interface (Amazon CLI) 修改维护时段的信息,请参阅 教程:更新维护时段 (Amazon CLI)

有关运行 Amazon CLI 命令以查看有关维护时段任务及其执行详细信息的练习,请转到教程:查看有关任务和任务执行的信息 (Amazon CLI)

关于教程命令输出

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

不过,您可以使用 Amazon 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  . :