Runs a shell, PowerShell, or batch command on one or more Elastic Compute Service (ECS) instances.
Description
Unlike the CreateCommand and InvokeCommand operation, RunCommand can be used to create and run a command in a single request.
When you call this operation, take note of the following items:
- The ECS instances on which you want to run a command must be instances that are deployed in virtual private clouds (VPCs).
- The instances must be in the
Running
state. - The Cloud Assistant client must be installed on the instances. For more information, see InstallCloudAssistant.
- Before you run a PowerShell command on a Windows instance, make sure that the PowerShell module is installed on the instance.
- If you want to specify a schedule by using a cron expression, you can set a time zone based on your requirements. If you do not set a time zone, the schedule is based on the system time of the instance. Make sure that the time or time zone of the instance meets your business needs. For information about time zones, see Configure the NTP service for ECS instances that run CentOS 6 or Configure the NTP service for Windows instances.
- You can specify the
TimeOut
parameter to set the timeout period for executions of the command on ECS instances. If an execution times out, the Cloud Assistant client forcefully terminates the command process.- When the one-time execution of a command times out, the execution state (InvokeRecordStatus) of the command becomes Failed.
-
For scheduled tasks, the timeout period takes effect on each execution of the command. The timeout of one execution does not affect the subsequent executions of the command. When a scheduled execution of a command times out, the execution state (InvokeRecordStatus) of the command becomes Failed.
To ensure that scheduled tasks can run as expected, make sure that the version of the Cloud Assistant client is not earlier than the following ones. A scheduled task can run a command at a specified interval, only once at a specified time, or at designated times based on a cron expression with a specified year or time zone. If the
ClientNeedUpgrade
error code is returned, you must upgrade the Cloud Assistant client to the latest version. For more information, see Update or disable updates for the Cloud Assistant client.- Linux: 2.2.3.282
- Windows: 2.1.3.282
- Command executions may fail due to instance status exceptions, network exceptions, or exceptions on the Cloud Assistant client. If an execution fails, no execution information is generated.
- When
EnableParameter
is set to true, the custom parameter feature is enabled. When you set theCommandContent
parameter, you can define custom parameters in the{{parameter}}
format. Then, when the command is run, the key-value pairs of the custom parameters are passed in. - You can retain up to 500 to 10,000 Cloud Assistant commands in each region based on your ECS usage. You can call the DescribeAccountAttribute operation to query quotas.
Debugging
Request parameters
Parameter | Type | Required | Example | Description |
---|---|---|---|---|
Action | String | Yes | RunCommand |
The operation that you want to perform. Set the value to RunCommand. |
RegionId | String | Yes | cn-hangzhou |
The region ID of the command. You can call the DescribeRegions operation to query the most recent region list. |
Name | String | No | testName |
The name of the command. The name supports all character sets and can be up to 128 characters in length. |
Description | String | No | testDescription |
The description of the command. The description supports all character sets and can be up to 512 characters in length. |
Type | String | Yes | RunShellScript |
The language type of the command. Valid values:
|
CommandContent | String | Yes | ZWNobyAxMjM= |
The content of the command. The command content can be plaintext or Base64-encoded. Take note of the following items:
|
WorkingDir | String | No | /home/ |
The working directory of the command on the ECS instance. Default value:
|
Timeout | Long | No | 3600 |
The timeout period for the command execution. Unit: seconds. A timeout error occurs when a command cannot be run because the process slows down or because a specific module or the Cloud Assistant client does not exist. When an execution times out, the command process is forcefully terminated. Default value: 60. |
EnableParameter | Boolean | No | false |
Specifies whether to include custom parameters in the command. Default value: false. |
RepeatMode | String | No | Once |
Specifies how to run the command. Valid values:
Default value:
Take note of the following items:
|
Timed | Boolean | No | true |
Specifies whether to run the command on a schedule. Valid values:
Default value: false. |
Frequency | String | No | 0 */20 * * * ? |
The schedule on which to run the command. You must specify this parameter when you
set
|
Parameters | Map | No | {"name":"Jack", "accessKey":"LTAIdyvdIqaRY****"} |
The key-value pairs of custom parameters to be passed in when the command includes
custom parameters. For example, assume that the command content is Number of custom parameters: 0 to 10. Take note of the following items:
This parameter is empty by default. You can leave this parameter empty to disable the custom parameter feature. |
KeepCommand | Boolean | No | false |
Specifies whether to retain the command after it is run. Valid values:
Default value: false. |
ContentEncoding | String | No | Base64 |
The encoding mode of command content (
Default value: PlainText. If the specified value of this parameter is invalid, PlainText is used by default. |
Username | String | No | root |
The username used to run the command on ECS instances.
You can also specify other usernames that already exist in the ECS instance to run the command. For security purposes, we recommend that you run Cloud Assistant commands as a regular user. For more information, see Configure a regular user to run Cloud Assistant commands. |
WindowsPasswordName | String | No | axtSecretPassword |
The name of the password used to run the command on a Windows instance. If you want to use a username other than the default System username to run the command
on the Windows instance, you must specify both the WindowsPasswordName and Note When you use the root username for Linux instances or the System username for Windows
instances to run the command, you do not need to specify the WindowsPasswordName parameter.
|
InstanceId.N | String | Yes | i-bp185dy2o3o6neg**** |
The ID of instance N on which to run the command. Valid values of N: 1 to 50. If one of the specified instances does not meet the conditions for running the command, the call fails. To ensure that the call is successful, specify only the IDs of instances that meet the conditions. |
Response parameters
Parameter | Type | Example | Description |
---|---|---|---|
RequestId | String | 473469C7-AA6F-4DC5-B3DB-A3DC0DE3**** |
The ID of the request. |
CommandId | String | c-7d2a745b412b4601b2d47f6a768d**** |
The ID of the command. |
InvokeId | String | t-7d2a745b412b4601b2d47f6a768d**** |
The ID of the task. |
Examples
Sample requests
https://ecs.aliyuncs.com/?Action=RunCommand
&CommandContent='echo hello'
&InstanceId.1=i-bp185dy2o3o6neg****
&InstanceId.2=i-bp541dc26ko6dd5****
&Name=Test
&RegionId=cn-hangzhou
&Type=RunShellScript
&Username=root
&<Common request parameters>
Sample success response
XML
format
HTTP/1.1 200 OK
Content-Type:application/xml
<RunCommandResponse>
<RequestId>E69EF3CC-94CD-42E7-8926-F133B863****</RequestId>
<CommandId>c-7d2a745b412b4601b2d47f6a768d****</CommandId>
<InvokeId>t-7d2a745b412b4601b2d47f6a768d****</InvokeId>
</RunCommandResponse>
JSON
format
HTTP/1.1 200 OK
Content-Type:application/json
{
"RequestId" : "E69EF3CC-94CD-42E7-8926-F133B863****",
"CommandId" : "c-7d2a745b412b4601b2d47f6a768d****",
"InvokeId" : "t-7d2a745b412b4601b2d47f6a768d****"
}
Error codes
HTTP status code | Error code | Error message | Description |
---|---|---|---|
400 | RegionId.ApiNotSupported | The api is not supported in this region. | The error message returned because the API operation cannot be called in the specified region. Check whether the RegionId parameter is valid. |
403 | InvalidCmdType.NotFound | The specified command type does not exist. | The error message returned because the specified Type parameter is invalid. |
403 | CmdContent.ExceedLimit | The length of the command content exceeds the upper limit. | The error message returned because the length of the command content exceeds the upper limit. |
403 | CmdName.ExceedLimit | The length of the command name exceeds the upper limit. | The error message returned because the command name exceeds 128 characters in length. |
403 | CmdDesc.ExceedLimit | The length of the command description exceeds the upper limit. | The error message returned because the command description exceeds 512 characters in length. |
403 | CmdCount.ExceedQuota | The total number of commands in the current region exceeds the quota. | The error message returned because the maximum number of Cloud Assistant commands in the specified region has been reached. |
403 | CmdParam.EmptyKey | Command parameters can not be empty. | The error message returned because the custom parameters are not passed in the command. |
403 | CmdParam.InvalidParamName | A command parameter name is invalid. | The error message returned because the name of a custom parameter in the command is invalid. |
403 | InvalidInstance.NotMatch | The specified instance type does not match the command. | The error message returned because the specified command cannot be run on the specified ECS instance. Check whether the state of the instance meets the conditions for running the Cloud Assistant command. |
403 | MissingParam.Frequency | The frequency must be specified when you create a timed task. | The error message returned because the Frequency parameter is not specified when you set Timed to true. |
403 | InvalidParam.Frequency | The specified frequency is invalid. | The error message returned because the specified Frequency parameter is invalid. Check whether the specified Frequency parameter is valid. |
403 | InstanceIds.ExceedLimit | The number of instance IDs exceeds the upper limit. | The error message returned because more than 50 ECS instance IDs are specified. |
403 | Invocation.ExceedQuota | The invocation quota in the current region has been reached for today. | The error message returned because the daily maximum number of command executions in the specified region has been reached. |
403 | Username.ExceedLimit | The length of the username exceeds the upper limit. | The error message returned because the length of the username exceeds the upper limit. |
403 | Operation.Forbidden | The operation is not permitted. | The error message returned because the operation is not supported. |
404 | InvalidRepeatMode.NotFound | The specified repeat mode does not exist. | The error message returned because the specified RepeatMode parameter does not exist. |
404 | InvalidInstance.NotFound | The specified instance does not exist. | The error message returned because the specified ECS instance does not exist. |
404 | InvalidCmdId.NotFound | The specified command ID does not exist. | The error message returned because the specified CommandId parameter is invalid. You can call the DescribeCommands operation to query all available command IDs. |
500 | InternalError.Dispatch | An error occurred when you dispatched the request. | The error message returned because an error occurred while the request was being sent. Try again later. |
For a list of error codes, visit the API Error Center.