All Products
Search
Document Center

Elastic Compute Service:Create a command

Last Updated:Dec 08, 2023

You can run Cloud Assistant commands to perform routine tasks on Elastic Compute Service (ECS) instances. These tasks include running automated O&M scripts, polling processes, resetting user passwords, installing or uninstalling software, updating applications, and installing patches. Command types can be batch or PowerShell for Windows instances, and shell for Linux instances. You can specify custom parameters as variables in Cloud Assistant commands.

Background information

  • 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. The commands that you create count against the Cloud Assistant quota of your account.

    Note

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

  • The maximum size of a Base64-encoded command is 18 KB.

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

  • When you create a command, make sure that the syntax, logic, or algorithm associated with the command is correct.

    For example, assume that you have created a /backup directory (mkdir /backup) on an instance. You can run the following shell commands to archive a file in this directory:

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

    In the preceding example, {{file}} is a custom parameter. Replace {{file}} with the name of the file that you want to archive. Example: /app/usrcredential. Custom parameters can be used 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 with the environment, such as AccessKey pairs, instance IDs, authorization codes, time parameters, and critical system files.

Procedure

Create a 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 upper-left corner of the top navigation bar, select a region. 地域

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

  5. In the Command Information section of the Create Command panel, configure the parameters described in the following table.

    Parameter

    Description

    Command Source

    Select the command source.

    • Enter Command Content: Create a command.

    • Select Saved Command: Select an existing command.

    Command Name

    Enter a name for the command.

    Execution Plan

    Select a plan on how to run the command.

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

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

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

    • Run on Schedule: The command is run at a specified interval, at a specified time, or on a schedule after you click Run or Execute and Save. If you set Implementation plan to Run on Schedule, the following 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 when 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 elapsed between two consecutive executions and is irrelevant to the amount of time required to run the command once. For example, you set the interval to 5 minutes and the amount of time required to run the command once is 2 minutes. Each time the command is run, the system waits 3 minutes before the system re-runs the command.

        • A task is not executed immediately after the task is created. 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 Execution Time to May 17, 2022, 17:30:50 and Time Zone 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: Use a cron expression to specify a schedule on which you want to run the command. Specify a schedule that is accurate to the second, minute, hour, 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 when you want to execute command execution tasks on a schedule. For more information about cron expressions, see Run a command on a schedule.

        Note

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

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

    Command Type

    Select a command type.

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

    • For Windows instances, select Bat or PowerShell.

    Command

    Enter or paste the content of the command.

    For information about sample shell commands, see View instance configurations.

    Use Parameters

    Specify whether to use parameters.

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

    You can specify built-in environment variables as custom parameters. Then, when you run this command, Cloud Assistant automatically replaces the values in the commands with the environment variables. You can specify the following built-in environment variables:

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

    • {{ACS::AccountId}}: the UID of the Alibaba Cloud account.

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

      Note

      When the command is run on multiple instances, if you want to specify {{ACS::InstanceId}} as a built-in environment variable, make sure that the version of the Cloud Assistant Agent is not earlier than one of the following ones:

      • Linux: 2.2.3.309

      • Windows: 2.1.3.309

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

      Note

      When the command is run on multiple instances, if you want to specify {{ACS::InstanceName}} as a built-in environment variable, make sure that the version of the Cloud Assistant Agent is not earlier than one of the following ones:

      • 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 {{ACS::InvokeId}} as a built-in environment variable, make sure that the version of the Cloud Assistant Agent is not earlier than one of the following ones:

      • Linux: 2.2.3.309

      • Windows: 2.1.3.309

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

      Note

      When you call the RunCommand operation, if you want to specify {{ACS::CommandId}} as a built-in environment variable, make sure that the version of the Cloud Assistant Agent is not earlier than one of the following ones:

      • Linux: 2.2.3.309

      • Windows: 2.1.3.309

    Command Description

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

    Username

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

    For security purposes, we recommend that you run Cloud Assistant commands based on the least privilege principle as a regular user. 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 Period

    Specify a timeout period for the command to run 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 Timeout Period to a value of less 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 Execute 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 Instances 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 Save.

Create a command by using Alibaba Cloud CLI

Sample request

Call the CreateCommand operation to create a Cloud Assistant command named update that is used to update the system of instances.

aliyun ecs CreateCommand --RegionId 'cn-hangzhou' \
--CommandContent 'eXVtIHVwZGF0ZSAteQ==' \
--Type 'RunShellScript' \
--Name 'update' \
--Description 'update' \
--output cols=CommandId
Note

Values enclosed in single quotation marks ('') are example values of the parameters. Configure the parameters based on actual conditions.

The following table describes the main parameters. For more information about parameters, see CreateCommand.

Parameter

Example

Description

RegionId

cn-hangzhou

The ID of the region in which to create the command.

Name

update

The name of the command.

Type

RunShellScript

The type of the command.

  • For Linux instances, set the value to RunShellScript.

  • For Windows instances, set the value to RunBatScript or RunPowershellScript.

CommandContent

eXVtIHVwZGF0ZSAteQ==

The Base64-encoded content of the command.

Description

update

The description of the command.

Sample response

CommandId
---------
c-hz018qng4on****

What to do next

After the command is created, you can view its details on the Commands tab. You can run the command on specific instances. For more information, see Run a command.

Note

If you turn on Use Parameters when you create a command, you must enter parameter values in the Command Parameters fields when you run the command.