All Products
Search
Document Center

Elastic Compute Service:Create and run a Cloud Assistant command

Last Updated:Mar 27, 2024

You can run Cloud Assistant commands to perform routine tasks on Elastic Compute Service (ECS) instances. The tasks include running automated O&M scripts, polling processes, resetting user passwords, installing or uninstalling software, updating applications, and installing patches. This topic describes how to create and run a Cloud Assistant command.

Prerequisites

  • The ECS instances on which you want to run a command are in the Running (Running) state.

  • Cloud Assistant Agent is installed on the instances. For more information, see Install Cloud Assistant Agent.

    To ensure that scheduled tasks can run as expected, make sure that the version of Cloud Assistant Agent is not earlier than the following versions. A scheduled task can run a command at a specific interval, run a command only once at a specific time, or run a command based on a schedule defined by a cron expression with a specific year or time zone.

    • Linux: 2.2.3.282

    • Windows: 2.1.3.282

    If the ClientNeedUpgrade error code is returned, update Cloud Assistant Agent to the latest version. For more information, see Upgrade or disable upgrades for Cloud Assistant Agent.

Limits

When you create and run a Cloud Assistant command, take note of the following items:

  • The size of the Base64-encoded script has the following limits:

    • If you want to instantly run and save the command by clicking Run and Save, the script cannot exceed 18 KB in size.

    • If you want to instantly run but not save the command by clicking Run, the script cannot exceed 24 KB in size.

  • You can call an API operation to run a command on up to 50 instances at the same time.

  • You can retain 500 to 10,000 Cloud Assistant commands in an Alibaba Cloud region. This quota may increase based on your ECS usage. For information about how to query quotas, see the Step 1: View resource quotas section in the "View and increase resource quotas" topic. If you click Run when you create a command in the Create Command panel, the command is not counted in your command quota.

    Note

    You can also call the DescribeAccountAttributes operation with the AttributeName.N parameter set to max-axt-command-count to query the maximum number of Cloud Assistant commands that you can retain in a region.

Procedure

Create and run a Cloud Assistant command in the ECS console

  1. Log on to the ECS console.

  2. In the left-side navigation pane, choose Maintenance & Monitoring > Cloud Assistant.

  3. In the top navigation bar, select the region and resource group to which the resource belongs. 地域

  4. In the upper-right corner, click Create/Run Command.

  5. In the Command Information section of the Create Command panel, configure the parameters. The following table describes the parameters.

    Parameter

    Description

    Command Source

    Select a command source.

    • Enter Command Content: Create a command.

    • Select Saved Command: Select an existing command.

    Command Type

    Select a command type.

    • For Linux instances, select Shell, Python, or Perl.

    • For Windows instances, select Bat or PowerShell.

    Command content

    Enter or paste command content.

    For information about shell commands, see View the system configurations of ECS instances.

    When you create a command, make sure that the syntax, logic, and algorithm of the command are correct.

    For example, to archive a file to the /backup directory (mkdir /backup) that you created on an instance, enter the following shell command:

    #!/bin/bash 
    OF=/backup/my-backup-$(date +%Y%m%d).tgz
    tar -cf $OF {{file}}
    Note

    In the preceding sample command, {{file}} is a custom parameter. When you run the command, you can set the custom parameter to the name of the file that you want to archive. Example: /app/usrcredential. You can use custom parameters in scenarios that require dynamic values and values that are shared across multiple commands. We recommend that you specify custom parameters for sensitive data or data that changes together with the environment, such as AccessKey pairs, instance IDs, authorization codes, time parameters, and critical system files.

    Use Parameters

    Specify whether to use parameters.

    If you turn on Use Parameters, specify custom parameters in the {{key}} format in the Command content field.

    Note

    You can specify up to 20 custom parameters in a single Cloud Assistant command.

    You can specify built-in environment parameters as custom parameters. When you run the command, the parameters are automatically specified by Cloud Assistant. You can specify the following built-in environment parameters:

    • {{ACS::RegionId}}: the ID of the region.

    • {{ACS::AccountId}}: the unique identifier (UID) of the Alibaba Cloud account.

    • {{ACS::InstanceId}}: the ID of the instance.

      Note

      If you want to run the command on multiple instances and specify the {{ACS::InstanceId}} parameter as a built-in environment parameter, make sure that the version of Cloud Assistant Agent is not earlier than the following versions. For more information, see Install Cloud Assistant Agent.

      • Linux: 2.2.3.309

      • Windows: 2.1.3.309

    • {{ACS::InstanceName}}: the name of the instance.

      Note

      If you want to run the command on multiple instances and specify the {{ACS::InstanceName}} parameter as a built-in environment parameter, make sure that the version of Cloud Assistant Agent is not earlier than the following versions. For more information, see Install Cloud Assistant Agent.

      • Linux: 2.2.3.344

      • Windows: 2.1.3.344

    • {{ACS::InvokeId}}: the ID of the command execution task.

      Note

      If you want to specify the {{ACS::InvokeId}} parameter as a built-in environment parameter, make sure that the version of Cloud Assistant Agent is not earlier than the following versions. For more information, see Install Cloud Assistant Agent.

      • Linux: 2.2.3.309

      • Windows: 2.1.3.309

    • {{ACS::CommandId}}: the ID of the command.

      Note

      If you want to specify the {{ACS::CommandId}} parameter as a built-in environment parameter when you call the RunCommand operation, make sure that the version of Cloud Assistant Agent is not earlier than the following versions. For more information, see Install Cloud Assistant Agent.

      • Linux: 2.2.3.309

      • Windows: 2.1.3.309

    Execution Plan

    Select a command execution plan.

    • Immediate execution: The command is immediately run after you click Run or Run and Save.

    • After the next startup of the system: The command is run the next time the associated instances are started after you click Run or Run and Save.

    • After each system startup: The command is run each time the associated instances are started after you click Run or Run and Save.

    • Run on Schedule: The command is run at a specific interval, at a specific time, or on a schedule after you click Run or Run and Save. The following execution schedule options are available:

      • Run at Fixed Interval: Use a rate expression to specify an interval at which you want to run the command. You can specify the interval in seconds, minutes, hours, or days. This option is suitable for scenarios in which you want to execute command execution tasks at a fixed interval.

        Note

        When you specify an interval, take note of the following limits:

        • The specified interval can range from 60 seconds to 7 days and must be longer than the timeout period of the scheduled task.

        • The interval is the amount of time that elapses between two consecutive executions. The interval is irrelevant to the amount of time that is required to run the command once. For example, you set the interval to 5 minutes, and the command requires 2 minutes to run once. Each time the command is run, the system waits for 3 minutes before it reruns the command.

        • After you create a task, the task does not immediately run. For example, you set the interval to 5 minutes and create a task to run the command. The task runs 5 minutes after it is created.

      • Run Only Once at Specified Time: Specify a point in time and a time zone to run the command only once.

        For example, if you set the Execution time parameter to May 17, 2022, 17:30:50 and the Time Zone parameter to (GMT+08:00) Asia/Shanghai, the command was run only once at 17:30:50 on May 17, 2022 (UTC+8).

      • Run on Clock-based Schedule Cron Expression: Use a cron expression to specify a schedule on which you want to run the command. Specify a schedule that is accurate to seconds, minutes, hours, day of the month, month, day of the week, or year, and select a time zone from the Time Zone drop-down list. The system calculates the schedule based on the cron expression and time zone and runs the command as scheduled. This option provides flexibility and is suitable for scenarios in which you want to execute command execution tasks on a schedule. For more information about cron expressions, see Cron expressions.

        Note

        The minimum interval must be 10 seconds or longer and cannot be shorter than the timeout period of scheduled executions.

        For example, if you set the Execution Frequency parameter to 0 0 12 ? * WED 2022 and the Time Zone parameter to (GMT+08:00) Asia/Shanghai, the system runs the command at 12:00:00 every Wednesday in 2022 (UTC+8).

    Command Name

    Enter a name for the command.

    Command Description

    Enter a description for the command. We recommend that you enter identifiable information, such as the purpose of the command, to facilitate management and maintenance.

    Username

    Specify a username that you want to use to run the command on ECS instances.

    For security purposes, we recommend that you run Cloud Assistant commands as a regular user based on the principle of least privilege. For more information, see Run Cloud Assistant commands as a regular user.

    By default, Cloud Assistant commands are run by the root user on Linux instances and by the system user on Windows instances.

    Execution Path

    Specify an execution path for the command. Different default execution paths are provided based on the operating system of instances on which the command is run.

    • For Linux instances, the default execution path is the /home directory of the root user.

    • For Windows instances, the default execution path is C:\Windows\system32.

    Timeout

    Specify a timeout period for running the command on instances. If a task that runs the command times out, Cloud Assistant forcefully terminates the task process.

    Unit: seconds. Default value: 60. Minimum value: 10. If you set the Timeout parameter to a value that is smaller than 10, the system changes the value to 10 to ensure that the command can be run.

    Tag

    Specify a tag that you want to add to the command for subsequent classification and management. Tag key: the key of the tag. Tag value: the value of the tag.

    Note
    • If you select instances and click Run and Save, the system adds the tag to the command and the command execution task.

    • If you select instances and click Run, the system adds the tag only to the command.

  6. In the Select Instance and Select Managed Instances sections, select the instances on which you want to run the command.

    Note

    A managed instance is an instance that is managed by Cloud Assistant but not provided by Alibaba Cloud. For more information, see Manage servers that are not provided by Alibaba Cloud.

  7. Click Run and Save or Run to immediately run the command task.

Create and run a Cloud Assistant command by using Alibaba Cloud CLI

Example on how to run a command only once

  • Sample requests:

    Call the RunCommand operation to create and run a Cloud Assistant command named update to update the operating systems of instances.

    aliyun ecs RunCommand --RegionId 'cn-hangzhou' \
    --Name 'update' --Username 'ecs-user' --Type 'RunShellScript' \
    --CommandContent 'eXVtIC15IHVwZGF0ZQ==' \
    --Timeout '60' --RepeatMode 'Once' --ContentEncoding 'Base64' \
    --InstanceId.1 'i-bp12e0ib2ztibede****'
    Note

    The values that are enclosed in single quotation marks ('') are sample values of the parameters. Specify the parameters based on your actual business scenario.

    The following table describes the parameters. For more information, see RunCommand.

    Parameter

    Example

    Description

    RegionId

    cn-hangzhou

    The region ID.

    Name

    update

    The name of the command.

    Username

    ecs-user

    The username that you want to use to run the command on ECS instances.

    Type

    RunShellScript

    The language type of the command.

    • For Linux instances, set this parameter to RunShellScript.

    • For Windows instances, set this parameter to RunBatScript or RunPowerShellScript.

    CommandContent

    eXVtIHVwZGF0ZSAteQ==

    The Base64-encoded content of the command.

    Timeout

    60

    The timeout period.

    RepeatMode

    Once

    The execution plan.

    ContentEncoding

    Base64

    The encoding format of the command content.

    InstanceId.1

    i-bp12e0ib2ztibede****

    The ID of ECS instance N on which to run the command. In this example, the value of N is 1.

  • Sample responses:

    {
            "CommandId": "c-hz018qlm868****",
            "InvokeId": "t-hz018qlm86d****",
            "RequestId": "1D24FA80-64DB-4842-AB20-25207994418F"
    }

Example on how to run a command on a schedule

  • Sample requests:

    Call the RunCommand operation to create and run a Cloud Assistant command named update to update the operating systems of instances at 12:00 every day in 2022 (UTC+8).

    aliyun ecs RunCommand --RegionId 'cn-hangzhou' \
    --Name 'update' --Description 'update' --Username 'ecs-user' --Type 'RunShellScript' \
    --CommandContent 'eXVtIC15IHVwZGF0ZQ==' \
    --Timeout '60' --RepeatMode 'Period' --ContentEncoding 'Base64' \
    --Frequency '0 0 12 * * ? 2022 Asia/Shanghai' \
    --InstanceId.1 'i-bp12e0ib2ztibede****'
    Note

    The values that are enclosed in single quotation marks ('') are sample values of the parameters. Specify the parameters based on your actual business scenario.

    The following table describes the parameters. For more information, see RunCommand.

    Parameter

    Example

    Description

    RegionId

    cn-hangzhou

    The region ID.

    Name

    update

    The name of the command.

    Description

    update

    The description of the command.

    Username

    ecs-user

    The username that you want to use to run the command on ECS instances.

    Type

    RunShellScript

    The language type of the command.

    • For Linux instances, set this parameter to RunShellScript.

    • For Windows instances, set this parameter to RunBatScript or RunPowerShellScript.

    CommandContent

    eXVtIC15IHVwZGF0ZQ==

    The Base64-encoded content of the command.

    Timeout

    60

    The timeout period.

    RepeatMode

    Period

    The execution plan.

    ContentEncoding

    Base64

    The encoding format of the command content.

    Frequency

    0 0 12 * * ? 2022 Asia/Shanghai'

    The schedule on which you want to run the command.

    InstanceId.1

    i-bp12e0ib2ztibede****

    The ID of ECS instance N on which to run the command. In this example, the value of N is 1.

  • Sample responses:

    {
            "CommandId": "c-hz018qlm868****",
            "InvokeId": "t-hz018qlm86d****",
            "RequestId": "1D24FA80-64DB-4842-AB20-25207994418F"
    }