Use Cloud Assistant exit codes to stop or restart ECS instances, with options for console, OpenAPI, and OOS batch operations.
Prerequisites
-
Instance is in Running state.
-
The Cloud Assistant Agent is installed on the target instance. View the status of Cloud Assistant. If the agent is not installed, see Install the Cloud Assistant Agent.
(Recommended) Stop or restart an instance with an exit code
Add an exit code to the end of your Cloud Assistant command to stop or restart an instance. Without the exit code, the Cloud Assistant Agent cannot report the execution result, and the command status may not update correctly.
The Cloud Assistant Agent on the target instance must be version:
-
Linux: 2.2.3.317
-
Windows: 2.2.3.317
If an error occurs, update the Cloud Assistant Agent to the latest version. See Upgrade or disable automatic upgrades of Cloud Assistant Agent.
In the upper-left corner of the page, select a region and resource group.
-
Click Create/Run Command.
-
In the Command Information section, configure the parameters. See Create and run commands.
-
At the end of the Command, add the corresponding exit code.
-
To stop an instance, use one of the following exit codes.
Operating system
Exit code
Command example
Linux
193
# This Shell command returns exit code 193, which triggers an action to stop the instance. exit 193Windows
3009
# This PowerShell command returns exit code 3009, which triggers an action to stop the instance. exit 3009 -
To restart an instance, use one of the following exit codes.
Operating system
Exit code
Command example
Linux
194
# This Shell command returns exit code 194, which triggers an action to restart the instance. exit 194Windows
3010
# This PowerShell command returns exit code 3010, which triggers an action to restart the instance. exit 3010
-
-
In the Select Instance or Select Managed Instances section, select the instances on which you want to run the command.
NoteA managed instance is not provided by Alibaba Cloud but is managed by Cloud Assistant. See Alibaba Cloud managed instances.
-
Click Run and Save or Run to immediately run the command.
Batch restart instances with OpenAPI
Run Python code in a local Linux environment to call OpenAPI operations that execute a command and restart instances in batches.
-
Prepare the required information.
-
Obtain the
AccessKeypair of the RAM user. See Create an access key pair. -
Call
DescribeRegionsto retrieve the list of regions. See DescribeRegions. -
Call
DescribeInstancesto filter instances that meet specified conditions. See DescribeInstances.
-
-
Configure the local environment and run the sample code.
-
Install or upgrade the Alibaba Cloud SDK for Python.
sudo pip install --upgrade alibabacloud_ecs20140526 -
Create a
.pyfile and add the following sample code.Click to view the sample code
Replace the following values in the sample code with your own:
-
AccessKey ID:
access_key = os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'] -
AccessKey secret:
access_key_secret = os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'] -
Region ID:
region_id = '<yourRegionId>' -
Instance IDs:
ins_ids= ["i-bp185fcs****","i-bp14wwh****","i-bp13jbr****"]
-
-
Run the
.pyfile.The following figure shows the result: a command runs on three instances to output
helloworld, then the instances restart automatically.
-
Batch restart instances with OOS
CloudOps Orchestration Service (OOS) automates O&M tasks through templates. Define your actions in a template and execute it to run commands and restart instances in batches.
-
Go to the template configuration page.
-
Log on to the OOS console.
-
Choose .
-
Click Create Template.
-
-
Complete the template configuration.
-
On the Create Template page, keep the default settings and click Next.
-
Click the YAML tab and enter the following code.
Click to view the sample code
-
Click Create Template.
-
In the dialog box that appears, enter the template name
runcommand_reboot_instancesand click OK.
-
-
Execute the template.
-
Find the template that you just created and click Create an execution in the Actions column.
-
Complete the execution configuration.
Select multiple instances on the Parameter Settings page and keep the default values for other settings.

-
On the OK page, click Create.
The template executes automatically. On the Basic Information page, wait until Execution Status changes to Successful.
-
-
View the execution process and task node details.
-
In Execution Steps And Results, click View Execution Flowchart to view the execution process.

-
Click the Cloud Assistant command execution step. On the loop task list tab, view the execution details of each task node.

-