This topic describes how to stop or restart Elastic Compute Service (ECS) instances by running a Cloud Assistant command.
(Recommended) Use an exit code to stop or restart instances
When you run a Cloud Assistant command to stop or restart instances, we recommend that you append a specific exit code to the end of the command to ensure real-time accuracy in the execution status of the command. Otherwise, Cloud Assistant Agent fails to report the execution results, and the command execution status may not be updated as expected.
Important
Make sure that Cloud Assistant Agent is not earlier than the following versions:
Linux: 2.2.3.317
Windows: 2.2.3.317
If an error is reported when you run a Cloud Assistant command with an exit code, upgrade Cloud Assistant Agent to the latest version. For more information, see Upgrade or disable upgrades of Cloud Assistant Agent.
Go to ECS console - ECS Cloud Assistant.
In the top navigation bar, select the region and resource group of the resource that you want to manage.
In the upper-right corner of the ECS Cloud Assistant page, click Create/Run Command.
In the Command Information section, configure the parameters. For more information, see Create and run a command.
In the Command content code editor, add an exit code to the end of the command script.
To stop instances, specify one of the exit codes in the following table based on the operating system type of the instances.
Operating system | Exit code | Example |
Operating system | Exit code | Example |
Linux | 193 |
# If the following shell command returns an exit code of 193, an operation is triggered to stop the specified instances.
exit 193
|
Windows | 3009 |
# If the following PowerShell command returns an exit code of 3009, an operation is triggered to stop the specified instances.
exit 3009
|
To restart instances by running a command, specify one of the exit codes in the following table based on the operating system type of the instances.
Operating system | Exit code | Example |
Operating system | Exit code | Example |
Linux | 194 |
# If the following shell command returns an exit code of 194, an operation is triggered to restart the specified instances.
exit 194
|
Windows | 3010 |
# If the following PowerShell command returns an exit code of 3010, an operation is triggered to restart the specified instances.
exit 3010
|
In the Select Instance or Select Managed Instances section, select the instances on which you want to run the command.
Note
A managed instance is an instance that is not provided by Alibaba Cloud but is managed by Cloud Assistant. For more information, see Alibaba Cloud managed instances.
Click Run and Save or Run to immediately run the command.
Use OOS to run a Cloud Assistant command to batch restart instances
CloudOps Orchestration Service (OOS) is an automated O&M service provided by Alibaba Cloud. You can use OOS templates to configure and execute O&M tasks.
Go to the Create Template page.
Log on to the OOS console.
In the left-side navigation pane, choose .
Click Create Template.
Configure the parameters.
In the Create Template step, retain the default settings and click Next Step.
Click the YAML tab and enter the following code.
Sample code
FormatVersion: OOS-2019-06-01
Description:
en: Bulky run command on ECS instances and reboot instance.
name-en: ACS-ECS-BulkyRunCommandRboot
categories:
- run_command
Parameters:
regionId:
Type: String
Description:
en: The id of region
Label:
en: Region
AssociationProperty: RegionId
Default: '{{ ACS::RegionId }}'
targets:
Type: Json
Label:
en: TargetInstance
AssociationProperty: Targets
AssociationPropertyMetadata:
ResourceType: ALIYUN::ECS::Instance
RegionId: regionId
commandType:
Description:
en: The type of command
Label:
en: CommandType
Type: String
AllowedValues:
- RunBatScript
- RunPowerShellScript
- RunShellScript
Default: RunShellScript
commandContent:
Description:
en: Command content to run in ECS instance
Label:
en: CommandContent
Type: String
MaxLength: 16384
AssociationProperty: Code
Default: echo hello
workingDir:
Description:
en: 'The directory where the created command runs on the ECS instances.Linux instances: under the home directory of the administrator (root user): /root.Windows instances: under the directory where the process of the Cloud Assistant client is located, such asC:\Windows\System32.'
Label:
en: WorkingDir
Type: String
Default: ''
timeout:
Description:
en: The value of the invocation timeout period of a command on ECS instances
Label:
en: Timeout
Type: Number
Default: 600
enableParameter:
Description:
en: Whether to include secret parameters or custom parameters in the command
Label:
en: EnableParameter
Type: Boolean
Default: false
username:
Description:
en: The username that is used to run the command on the ECS instance
Label:
en: Username
Type: String
Default: ''
windowsPasswordName:
Description:
en: The name of the password used to run the command on a Windows instance
Label:
en: WindowsPasswordName
Type: String
Default: ''
AssociationProperty: SecretParameterName
rateControl:
Description:
en: Concurrency ratio of task execution
Label:
en: RateControl
Type: Json
AssociationProperty: RateControl
Default:
Mode: Concurrency
MaxErrors: 0
Concurrency: 10
OOSAssumeRole:
Description:
en: The RAM role to be assumed by OOS
Label:
en: OOSAssumeRole
Type: String
Default: OOSServiceRole
RamRole: '{{ OOSAssumeRole }}'
Tasks:
- Name: getInstance
Description:
en: Views the ECS instances.
Action: ACS::SelectTargets
Properties:
ResourceType: ALIYUN::ECS::Instance
RegionId: '{{ regionId }}'
Filters:
- '{{ targets }}'
Outputs:
instanceIds:
Type: List
ValueSelector: Instances.Instance[].InstanceId
- Name: runCommand
Action: ACS::ECS::RunCommand
Description:
en: Execute cloud assistant command.
Properties:
regionId: '{{ regionId }}'
commandContent: '{{ commandContent }}'
instanceId: '{{ ACS::TaskLoopItem }}'
commandType: '{{ commandType }}'
workingDir: '{{ workingDir }}'
timeout: '{{ timeout }}'
enableParameter: '{{ enableParameter }}'
username: '{{ username }}'
windowsPasswordName: '{{ windowsPasswordName }}'
Loop:
RateControl: '{{ rateControl }}'
Items: '{{ getInstance.instanceIds }}'
Outputs:
commandOutputs:
AggregateType: Fn::ListJoin
AggregateField: commandOutput
Outputs:
commandOutput:
Type: String
ValueSelector: invocationOutput
- Name: rebootInstance
Action: ACS::ECS::RebootInstance
Description:
en: Restarts the ECS instances.
Properties:
regionId: '{{ regionId }}'
instanceId: '{{ ACS::TaskLoopItem }}'
Loop:
RateControl: '{{ rateControl }}'
Items: '{{ getInstance.instanceIds }}'
Outputs:
instanceIds:
Type: List
Value: '{{ getInstance.instanceIds }}'
Click Create Template.
In the dialog box that appears, enter a template name and click OK. In this example, the template name is runcommand_reboot_instances
.
Execute the template.
Find the template that you created and click Create Execution in the Actions column.
Configure the execution.
Complete the execution configurations based on the on-screen instructions. In the Parameter Settings step, set the TargetInstance parameter to Manually Select Instances and select multiple instances. Use the default values for other parameters.
In the OK step, click Create.
After the execution is created, the execution starts and you are automatically directed to the Basic Information tab on the execution details page. If the template is executed, Success is displayed next to Execution Status.
View the execution procedure and the details of each task node.
In the Execution Steps and Results section, click View Execution Flowchart to view the execution process.

Click the Execute cloud assistant command step and then click the circular Task List tab to view the execution details of each task node. The following figure shows that the operations specified in the template are performed.
