RunCommand_云服务器 ECS_API文档

插件下载了解更多
VS Code 插件
安装插件之前,确保已安装 VS Code安装插件之前,确保已安装 VS Code
Alibaba Cloud Developer Toolkit is a collection of extensions that can help access Alibaba Cloud services in Visual Studio Code.
JetBrains 插件
安装插件之前,确保已安装 JetBrains IDE安装插件之前,确保已安装 JetBrains IDE
The Alibaba Cloud Developer Toolkit for JetBrains makes it easier to access Alibaba Cloud services.

接口说明

该接口为异步接口,当前请求发送成功后,您可以通过返回的命令 ID 或命令执行 ID 调用 DescribeInvocations 或者 DescribeInvocationResults 查询执行结果。

使用须知

  • 目标实例的状态为运行中(Running),您可以调用 DescribeInstances 查询。
  • 目标实例已经预先安装云助手 Agent,您可以通过 InstallCloudAssistant 进行安装,并通过 DescribeCloudAssistantStatus 查询安装状态。
    说明 2017 年 12 月 01 日之后使用公共镜像创建的 ECS 实例,默认预装了云助手 Agent。
  • 执行 PowerShell 类型的命令时,您需要确保目标 ECS 实例的 Windows 操作系统已经配置了 PowerShell 模块。

注意事项

  • 在一个地域下,您最多可以保有 500~50,000 条云助手命令,您也可以申请提升配额,请参见配额管理

  • 云助手 Agent 版本需要不低于以下对应的版本才能支持定时任务的新特性(固定时间间隔执行、仅在指定时间执行一次、基于 Cron 表达式定时执行时指定年份或时区)。如果结果返回ClientNeedUpgrade错误码,请参见升级或禁止升级云助手 Agent,将客户端更新至最新版本。

    - Linux:2.2.3.282。
    - Windows:2.1.3.282。
    
  • 当您基于 Cron 表达式执行定时任务且指定了时区,时钟定时执行时间设置基准为您指定的时区;当您没有指定时区时,时钟定时执行时间设置基准为 ECS 实例内的系统时区,且执行时间以实例的系统时间为准。请确保 ECS 实例的时间或者时区与您预期的时间一致。关于时区的更多信息,请参见设置 Linux 实例时区和 NTP 服务设置 Windows 实例 NTP 服务

使用建议

  • 超时设置:您可以通过指定参数Timeout为命令设置在 ECS 实例中执行时最大的超时时间,命令执行超时后,云助手 Agent 会强制终止进程。
    • 单次执行超时后,命令的执行状态( InvokeRecordStatus )变为执行失败(Failed)。
    • 定时执行的超时时间对每一次执行记录均有效,上次执行超时不影响下一次执行。某次执行超时后,执行状态( InvokeRecordStatus )变为执行失败(Failed)。
  • 执行失败:命令可能会因为目标实例的状态异常、网络异常或云助手 Agent 异常而出现无法执行的情况,无法执行时不会生成执行信息。更多信息,请参见执行失败常见错误及修复建议
  • 自定义参数EnableParameter=true时会启用自定义参数功能。在设置CommandContent时可以通过{{parameter}}的形式表示自定义参数,并在运行命令时,传入自定义参数键值对。

流控信息

请求速率为1000/60(s)。更多流控信息,请前往配额中心查看

授权信息

如下是此API对应的授权信息,用于RAM权限策略语句的Action元素中,为RAM用户或RAM角色授予调用此API的权限。请通过 RAM 访问控制设置,使用方法可参考访问控制帮助文档

具体说明如下:展开详情

操作访问级别资源类型条件关键字关联操作
ecs:RunCommand
Update
Instance
acs:ecs:{#regionId}:{#accountId}:instance/{#instanceId}
ecs:CommandRunAs

请求参数

字段名称字段详情
RegionIdstring

地域 ID。您可以调用 DescribeRegions 查看最新的阿里云地域列表。

示例值:cn-hangzhou参考取值来源:
    DescribeRegions
ResourceGroupIdstring

命令执行的资源组 ID,当指定该参数时:展开详情

示例值:rg-bp67acfmxazb4p****参考取值来源:
    DescribeSecurityGroups
Namestring

命令名称。支持全字符集,长度不得超过 128 个字符。

示例值:testName
Descriptionstring

命令描述。支持全字符集,长度不得超过 512 个字符。

示例值:testDescription
Typestring

命令类型。取值范围:展开详情

示例值:RunShellScript
CommandContentstring

命令内容。命令内容可以是明文内容或 Base64 编码后的内容。您需要注意:展开详情

示例值:ZWNobyAxMjM=
WorkingDirstring

命令在 ECS 实例中的运行目录。长度不得超过 200 个字符。展开详情

示例值:/home/user
Timeoutinteger<int64>

执行命令的超时时间,单位:秒。展开详情

注意 该字段类型为 Long,在序列化/反序列化的过程中可能导致精度丢失,请注意数值不得大于 9007199254740991。
示例值:3600
EnableParameterboolean

命令中是否包含自定义参数。展开详情

示例值:false
RepeatModestring

设置命令执行的方式。取值范围:展开详情

示例值:Once
Timedboolean
说明 该参数已废弃,传入该参数不会生效。
示例值:true
Frequencystring

定时执行命令的执行时间。目前支持三种定时执行方式:固定时间间隔执行(基于 Rate 表达式)、仅在指定时间执行一次、基于时钟定时执行(基于 Cron 表达式)。展开详情

示例值:0 */20 * * * ?
Parametersobject

命令中包含自定义参数时,执行命令时传入的自定义参数的键值对。例如,命令内容为echo {{name}},则可以通过Parameter参数传入键值对{"name":"Jack"}。自定义参数将自动替换变量值name,得到一条新的命令,实际执行的是echo Jack展开详情

示例值:{"name":"Jack", "accessKey":"LTAI*************"}
KeepCommandboolean

执行完该命令后,是否保留下来。取值范围:展开详情

示例值:false
ContentEncodingstring

命令内容(CommandContent)的编码方式。取值范围(不区分大小写):展开详情

示例值:Base64
Usernamestring

在 ECS 实例中执行命令的用户名称。长度不得超过 255 个字符。展开详情

示例值:test
WindowsPasswordNamestring

在 Windows 实例中执行命令的用户的密码名称。长度不得超过 255 个字符。展开详情

示例值:axtSecretPassword
InstanceIdarray<string>

ECS 实例 ID 数组,数组长度:1~100。展开详情

示例值:i-bp185dy2o3o6neg****子级条数 <= 501
Tagarray<object>

标签对数组,数组长度:0~20。

子级条数 <= 21
ContainerIdstring

容器 ID。仅支持 64 位 16 进制字符串,允许存在docker://containerd://或者cri-o://前缀来明确指定的容器运行。展开详情

示例值:ab141ddfbacfe02d9dbc25966ed971536124527097398d419a6746873fea****
ContainerNamestring

容器名称。展开详情

示例值:test-container
ClientTokenstring

保证请求幂等性。从您的客户端生成一个参数值,确保不同请求间该参数值唯一。ClientToken 只支持 ASCII 字符,且不能超过 64 个字符。更多信息,请参见如何保证幂等性

示例值:123e4567-e89b-12d3-a456-426655440000
ResourceTagarray<object>

用于筛选实例的标签数组,数组长度:0~20。可以在不指定 InstanceId 的情况下,向具有相同标签的实例批量执行命令。

子级条数 <= 11
TerminationModestring

停止任务(手动停止或执行超时打断)时的模式。可能值:展开详情

示例值:ProcessTree
Launcherstring

脚本执行的引导程序。长度不能超过 1 KB。

示例值:python3 -u {{ACS::ScriptFileName|Ext(".py")}}

返回参数

字段名称字段详情
RequestIdstring

请求 ID。

示例值:473469C7-AA6F-4DC5-B3DB-A3DC0DE3****
CommandIdstring

命令 ID。

示例值:c-7d2a745b412b4601b2d47f6a768d****
InvokeIdstring

命令执行 ID。

示例值:t-7d2a745b412b4601b2d47f6a768d****

返回示例

错误码

全局错误码
HTTP 状态码错误码错误信息操作
400
RegionId.ApiNotSupported
The api is not supported in this region.诊断
400
MissingParam.InstanceId
The parameter instanceId is missing or empty.诊断
400
NumberExceed.Tags
Ensure the number of tag parameters is not greater than 20.诊断
400
InvalidTagValue.Malformed
The specified Tag.n.Value is not valid.诊断
400
Duplicate.TagKey
The Tag.N.Key contain duplicate key.诊断
400
InvalidTagKey.Malformed
The specified Tag.n.Key is not valid.诊断
400
MissingParameter.TagKey
You must specify Tag.N.Key.诊断
400
InvalidContainerId.Malformed
The specified parameter ContainerId is not valid.诊断
400
InvalidContainerName.Malformed
The specified parameter ContainerName is not valid.诊断
400
InvalidClientToken.Malformed
The specified parameter clientToken is not valid.诊断
400
CmdParam.EmptyKey
Command parameters can not be empty.诊断
400
CmdParam.InvalidParamName
A command parameter name is invalid.诊断
400
CmdContent.DecodeError
The CommandContent can not be base64 decoded.诊断
400
InvalidInstance.NotMatch
The specified instance type does not match the command.诊断
400
MissingParam.Frequency
The frequency must be specified when you create a timed task.诊断
400
InvalidParam.Frequency
The specified frequency is invalid.诊断
400
ParameterKey.Duplicate
The parameter may not contain duplicate keys.诊断
400
Parameter.NotMatched
The parameters of creation do not match those of invocation.诊断
400
WindowsPasswordName.Missed
WindowsPasswordName must be specified when you create a Windows task.诊断
400
Parameter.Disabled
Parameters should not be passed when CreateCommand.EnableParameter is false.诊断
400
InvalidParameter.WorkingDir
The specified parameter WorkingDir is not valid.诊断
400
NumberExceed.ResourceTags
The maximum number of ResourceTags is exceeded.诊断
400
MissingParameter.ResourceTagKey
You must specify ResourceTag.N.Key.诊断
400
InvalidResourceTagKey.Malformed
The specified ResourceTag key is not valid.诊断
400
InvalidResourceTagValue.Malformed
The specified ResourceTag value is not valid.诊断
400
Duplicate.ResourceTagKey
The ResourceTag contains duplicate keys.诊断
400
InvalidResourceTag.InstanceNotFound
InstanceIds are not found by the specified ResourceTag.诊断
400
InvalidResourceTag.ConflictWithInstanceIds
The specified param ResourceTag conflicts with InstanceId.诊断
403
CmdContent.ExceedLimit
The length of the command content exceeds the upper limit.诊断
403
CmdName.ExceedLimit
The length of the command name exceeds the upper limit.诊断
403
CmdDesc.ExceedLimit
The length of the command description exceeds the upper limit.诊断
403
CmdCount.ExceedQuota
The total number of commands in the current region exceeds the quota.诊断
403
CmdParamCount.ExceedLimit
You've reached the limit on the count of command parameters.诊断
403
CmdParamName.ExceedLimit
The length of the command parameter name exceeds the limit.诊断
403
InstanceIds.ExceedLimit
The number of instance IDs exceeds the upper limit.诊断
403
Invocation.ExceedQuota
The invocation quota in the current region has been reached for today.诊断
403
ParameterCount.ExceedLimit
The number of command parameters exceeds the maximum number that can be set.诊断
403
ParameterKey.ExceedLimit
The length of the specified parameter key exceeds the maximum length that can be set.诊断
403
ParameterType.NotSupported
The type of parameter value is not supported.诊断
403
Username.ExceedLimit
The length of the username exceeds the upper limit.诊断
403
WindowsPasswordName.ExceedLimit
The length of the WindowsPasswordName exceeds the upper limit.诊断
403
ParameterStore.NotSupported
Parameter Store is not supported in this region.诊断
403
TemporaryAccessKey.Error
The temporary accessKey is invalid.诊断
403
ParameterStore.InvalidParameters
The parameter is invalid in Parameter Store.诊断
403
ParameterStore.NoPermission
You have no access to Parameter Store.诊断
403
OperationDenied.BidOwnResource
Bid user can not own resource.诊断
403
Operation.Forbidden
The operation is not permitted.诊断
403
IdempotentParameterMismatch
The specified parameter has changed while using an already used clientToken.诊断
403
IdempotentProcessing
The previous idempotent request(s) is still processing.诊断
403
InvalidStatus.ResourceGroup
You cannot perform an operation on a resource group that is being created or deleted.诊断
403
InvalidParameterCharacter.CommandName
The command Name contains illegal characters.诊断
403
InvalidParameterCharacter.CommandDescription
The command Description contains illegal characters.诊断
403
InvalidParameterCharacter.CommandWorkingDir
The command WorkingDir contains illegal characters.诊断
403
InvalidLauncher.LengthLimitExceeded
The length of the parameter Launcher exceeds the limit of 1 KB characters.诊断
403
InvalidParameterCharset.Parameters
The parameter Parameters contains illegal charset.诊断
404
InvalidCmdType.NotFound
The specified command type does not exist.诊断
404
InvalidRepeatMode.NotFound
The specified repeat mode does not exist.诊断
404
InvalidRegionId.NotFound
The RegionId provided does not exist in our records.诊断
404
InvalidInstance.NotFound
The specified instance does not exist.诊断
404
InvalidCmdId.NotFound
The specified command ID does not exist.诊断
404
InvalidResourceGroup.NotFound
The ResourceGroup provided does not exist in our records.诊断
404
InvalidTerminationMode.NotFound
The specified parameter TerminationMode does not exist.诊断
500
InternalError.Dispatch
An error occurred when you dispatched the request.诊断

变更历史

变更时间变更内容概要操作
2024-10-28
变更错误码403
2024-08-01
变更错误码403
新增请求参数Launcher
2024-05-14
变更错误码404
新增请求参数TerminationMode
2024-05-11
变更错误码403
2024-04-12
变更请求参数InstanceId

相关示例

执行云助手命令
2021-01-15