

# 启动 CloudWatch 代理
<a name="start-CloudWatch-Agent-on-premise-SSM-onprem"></a>

您可以使用 Systems Manager Run Command 或命令行启动 CloudWatch 代理。

有关如何在启用了安全增强型 Linux (SELinux) 的系统上设置代理的信息，请参阅[通过安全增强型 Linux (SELinux) 设置 CloudWatch 代理](CloudWatch-Agent-SELinux.md)。

## 使用命令行在 Amazon EC2 上启动 CloudWatch 代理
<a name="start-CloudWatch-Agent-EC2-commands-fleet"></a>

按照以下步骤使用命令行在 Amazon EC2 上启动 CloudWatch 代理。

有关如何在启用了安全增强型 Linux (SELinux) 的系统上设置代理的信息，请参阅[通过安全增强型 Linux (SELinux) 设置 CloudWatch 代理](CloudWatch-Agent-SELinux.md)。

**使用命令行在 Amazon EC2 上启动 CloudWatch 代理**

1. 将要使用的代理配置文件复制到要运行代理的服务器。记下将其复制到的路径名。

1. 此命令中，`-a fetch-config` 会使代理加载最新版本的 CloudWatch 代理配置文件，`-s` 则会启动该代理。

   输入下列命令之一。将 *configuration-file-path* 替换为指向代理配置文件的路径。如果您使用向导创建此文件，则此文件命名为 `config.json`；如果您手动创建该文件，则该文件可能命名为 `amazon-cloudwatch-agent.json`。

   在运行 Linux 的 EC2 实例上，输入以下命令。

   ```
   sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:configuration-file-path
   ```

   在运行 Linux 的本地服务器上，输入以下内容：

   ```
   sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m onPremise -s -c file:configuration-file-path
   ```

   在运行 Windows Server 的 EC2 实例上，从 PowerShell 控制台中输入以下内容：

   ```
   & "C:\Program Files\Amazon\AmazonCloudWatchAgent\amazon-cloudwatch-agent-ctl.ps1" -a fetch-config -m ec2 -s -c file:configuration-file-path
   ```

   在运行 Windows Server 的本地服务器上，从 PowerShell 控制台中输入以下内容：

   ```
   & "C:\Program Files\Amazon\AmazonCloudWatchAgent\amazon-cloudwatch-agent-ctl.ps1" -a fetch-config -m onPremise -s -c file:configuration-file-path
   ```

## 在本地服务器上启动 CloudWatch 代理
<a name="start-CloudWatch-Agent-on-premises"></a>

按照以下步骤在本地服务器上启动 CloudWatch 代理。

**使用 SSM Agent 在本地部署服务器上启动 CloudWatch 代理**

1. 通过 [https://console.aws.amazon.com/systems-manager/](https://console.amazonaws.cn/systems-manager/) 打开 Systems Manager 控制台。

1. 在导航窗格中，选择 **Run Command**。

   –或者–

   如果打开了 Amazon Systems Manager 主页，请向下滚动并选择 **Explore Run Command（浏览 Run Command）**。

1. 选择 **Run command（运行命令）**。

1. 在**命令文档**列表中，选择 **AmazonCloudWatch-ManageAgent** 旁边的按钮。

1. 在**目标**区域，选择安装了该代理的实例。

1. 在**操作**列表中，选择**配置**。

1. 在**模式**列表中，选择 **onPremise**。

1. 在 **Optional Configuration Location（可选的配置位置）**框中，输入您使用向导创建并存储在 Parameter Store 中的代理配置文件的名称。

1. 选择**运行**。

   该代理使用在配置文件中指定的配置启动。

**使用命令行在本地部署服务器上启动 CloudWatch 代理**
+ 此命令中，`-a fetch-config` 会使代理加载最新版本的 CloudWatch 代理配置文件，`-s` 则会启动该代理。

  Linux：如果在 Systems Manager Parameter Store 中保存了配置文件，请输入以下命令：

  ```
  sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m onPremise -s -c ssm:configuration-parameter-store-name
  ```

  Linux：如果在本地计算机上保存了配置文件，请输入以下命令。将 *configuration-file-path* 替换为指向代理配置文件的路径。如果您使用向导创建此文件，则此文件命名为 `config.json`；如果您手动创建该文件，则该文件可能命名为 `amazon-cloudwatch-agent.json`。

  ```
  sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m onPremise -s -c file:configuration-file-path
  ```

  Windows Server：如果在 Systems Manager Parameter Store 中保存代理配置文件，请从 PowerShell 控制台中输入以下命令：

  ```
  & "C:\Program Files\Amazon\AmazonCloudWatchAgent\amazon-cloudwatch-agent-ctl.ps1" -a fetch-config -m onPremise -s -c ssm:configuration-parameter-store-name
  ```

  Windows Server：如果在本地计算机上保存了代理配置文件，请从 PowerShell 控制台中输入以下命令。将 *configuration-file-path* 替换为指向代理配置文件的路径。如果您使用向导创建此文件，则此文件命名为 `config.json`；如果您手动创建该文件，则该文件可能命名为 `amazon-cloudwatch-agent.json`。

  ```
  & "C:\Program Files\Amazon\AmazonCloudWatchAgent\amazon-cloudwatch-agent-ctl.ps1" -a fetch-config -m onPremise -s -c file:configuration-file-path
  ```