This topic describes how to stop or restart Elastic Compute Service (ECS) instances by running a Cloud Assistant command.
Prerequisites
The instances that you want to stop or restart are in the Running (Running) state.
Cloud Assistance Agent is installed on the instances. For more information, see Install Cloud Assistant Agent.
(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. If you run a Cloud Assistant command without an exit code to stop or restart instances, the execution status of the command may not be correctly updated even if the command finishes with the stop or restart operation. This occurs because Cloud Assistant Agent does not save the execution status of the command before the command is run to stop or restart the instances.
Make sure that the installed version of Cloud Assistant Agent is not earlier than the following versions:
Linux: 2.2.3.317
Windows: 2.1.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 for Cloud Assistant Agent.
Log on to the ECS console.
In the left-side navigation pane, choose .
In the top navigation bar, select the region and resource group to which the resource belongs.
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 Cloud Assistant command.
In the Command content code editor, add an exit code to the end of the command script.
To stop 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
Sample command
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
Sample command
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.
NoteA 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.
Call API operations to run a Cloud Assistant command to batch restart instances
Alibaba Cloud provides a variety of API operations for you to manage your cloud resources. This section describes how to call API operations by running Python code in an on-premises Linux environment to run a Cloud Assistant command to batch restart instances.
Prepare the information required to run a Cloud Assistant command.
Obtain an AccessKey pair.
We recommend that you obtain the AccessKey pair of a Resource Access Management (RAM) user. For more information, see Create an AccessKey pair.
Obtain the region ID of the instances on which you want to run the Cloud Assistant command.
You can call the DescribeRegions operation to query the most recent region list. For information about the parameters in the DescribeRegions operation, see DescribeRegions.
Obtain the IDs of the instances on which you want to run the Cloud Assistant command.
You can call the DescribeInstances operation to query the instances that meet specific conditions. For example, you can query the instances that are in the Running state or have specific tags added. For information about the parameters in the DescribeInstances operation, see DescribeInstances.
Configure the on-premises environment and run the sample code.
Install Alibaba Cloud ECS SDK for Python.
sudo pip install aliyun-python-sdk-ecs
Update ECS SDK for Python to the latest version.
sudo pip install --upgrade aliyun-python-sdk-ecs
Create a
.py
file and write the following sample code to the file.Replace the following parameters in the sample code with the actual values obtained in the preceding step:
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****"]
Sample code:
Run the
.py
file.The following figure shows a sample result after the .py file is run. In this example, a command is run on three instances and
helloworld
is returned, and then the three instances are restarted.
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.
Click the YAML tab and enter the following code.
Sample code:
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 as prompted. 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.