Stop or restart an instance with a Cloud Assistant command
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 output shows the result. The command
helloworldis run on 3 instances, and the instances are automatically restarted:[root@iZxxx ~]# vim runcommand_reboot_instances.py [root@iZxxx ~]# ls runcommand_reboot_instances.py [root@iZxxx ~]# python runcommand_reboot_instances.py /usr/lib/python2.7/site-packages/aliyunsdkcore/auth/algorithm/sha_hmac256.py:20: CryptographyDeprecationWarning: Python 2 is now deprecated in cryptography, and will be removed in the next release. from cryptography.hazmat.backends import default_backend 09-18 14:33 root [INFO]: run command,invoke-id:t-hz0xxxxx 09-18 14:34 root [INFO]: instance i-bp185fcs**** command execute finished,status: Finished,output:helloworld 09-18 14:34 root [INFO]: instance i-bp14wwh**** command execute finished,status: Finished,output:helloworld 09-18 14:34 root [INFO]: instance i-bp13jbr**** command execute finished,status: Finished,output:helloworld 09-18 14:34 root [WARNING]: reboot instance Now 09-18 14:39 root [WARNING]: reboot instance Finished 09-18 14:39 root [INFO]: instance i-bp185fcs**** status: Running 09-18 14:39 root [INFO]: instance i-bp14wwh**** status: Running 09-18 14:39 root [INFO]: instance i-bp13jbr**** status: Running
-
Batch restart instances with OOS
CloudOps Orchestration Service (OOS) automates O&M tasks through templates. Define your actions in a template, then 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.
Follow the prompts to complete the configuration. On the Parameter Settings page, select multiple instances and keep other settings as default. The example uses the following parameter settings:
-
Region: Select China (Hangzhou)
-
Target Instances: Choose Select Instances Manually, then click Select Instances to add the ECS instances on which to run the command
-
Command Type: Select RunShellScript
-
Command Content: Enter
echo hello
-
-
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. The execution flowchart shows that the task passes through Get ECS Instances, Run Cloud Assistant Command, and Restart Instances in sequence, all of which have been executed successfully.
-
Click the Cloud Assistant command execution step. On the loop task list tab, view the execution details of each task node. All scheduled operations have been completed successfully.
-