This topic uses CentOS as an example to describe how to use a Shell script to automatically deploy applications on ECS instances created by Auto Scaling.
- Runlevel 0: the halt runlevel.
- Runlevel 1: causes the system to start up in a single user mode under which only the root user can log on.
- Runlevel 2: boots the system into the multi-user mode with text-based console logon capability. This runlevel does not start the network.
- Runlevel 3: boots the system into the multi-user mode with text-based console logon capability. This runlevel starts the network.
- Runlevel 4: undefined runlevel. This runlevel can be configured to provide a custom boot state.
- Runlevel 5: boots the system into the multi-user mode. This runlevel starts the graphical desktop environment at the end of the boot process.
- Runlevel 6: reboots the system.
You can use a script to automatically install or update applications or run specific code on ECS instances created by Auto Scaling. To do so, add the script to a custom image and configure a command to run the script when the operating system boots. Then, select the custom image in a scaling configuration. After an ECS instance is created based on the scaling configuration, the script is automatically run on the ECS instance.
CentOS 6 and earlier versions use System V init as the initialization system, whereas CentOS 7 uses Systemd as the initialization system. System V init and Systemd are quite different in the ways that they operate. This topic describes how to configure a script in CentOS 6 and earlier versions and in CentOS 7 respectively.
CentOS 6 and earlier versions
This section describes how to configure a script in CentOS 6 and earlier versions.
- Create a Shell script for testing.
#! /bin/sh # chkconfig: 6 10 90 # description: Test Service echo "hello world!"
The CHKCONFIG command in the preceding script sets the runlevel and priorities for running the script when the operating system boots and shuts down. The value 6 indicates runlevel 6, which means that the script is run when the operating system reboots. For more information about runlevels, see the background information in this topic. The value 10 indicates the priority for running the script when the operating system boots. The value 90 indicates the priority for running the script when the operating system shuts down. A priority ranges from 0 to 100, where a higher value indicates a lower priority.Note To make sure that the ECS instance is released only after the script is run on the ECS instance, change runlevel 6 to runlevel 0 in the preceding script.
- Place the test script in the /etc/rc.d/init.d/ directory and run the
chkconfig --level 6 test oncommand. Then, the script is run each time the operating system reboots.Note To make sure that the ECS instance is released only after the script is run on the ECS instance, change runlevel 6 to runlevel 0 in the preceding script. Then, the script is run each time the operating system shuts down.
cd /tmp echo "phpwind" yum install -y \ unzip \ wget \ httpd \ php \ php-fpm \ php-mysql \ php-mbstring \ php-xml \ php-gd \ php-pear \ php-devel chkconfig php-fpm on \ && chkconfig httpd on wget http://pwfiles.oss-cn-hangzhou.aliyuncs.com/com/soft/phpwind_v9.0_utf8.zip \ && unzip -d pw phpwind_v9.0_utf8.zip \ && mv pw/phpwind_v9.0_utf8/upload/* /var/www/html \ && wget http://ess.oss-cn-hangzhou.aliyuncs.com/ossupload_utf8.zip -O ossupload_utf8.zip \ && unzip -d ossupload ossupload_utf8.zip \ && /bin/cp -rf ossupload/ossupload_utf8/* /var/www/html/src/extensions/ \ && chown -R apache:apache /var/www/html service httpd start && service php-fpm start echo "Install CloudMonitor" wget http://update2.aegis.aliyun.com/download/quartz_install.sh chmod +x quartz_install.sh bash quartz_install.sh echo "CloudMonitor installed"
This section describes how to configure a script in CentOS 7. CentOS 7 uses Systemd as the initialization system. After you configure a script by following the steps in this section, the script can be run when the system is shut down.
- Create the script to run.
- Create the run-script-when-shutdown.service file in the /etc/systemd/system directory.
Add the following content to the file. Change the value of the ExecStop variable to the absolute path of the script to run.
[Unit] Description=service to run script when shutdown After=syslog.target network.target [Service] Type=simple ExecStart=/bin/true ExecStop=/path/to/script/to/run RemainAfterExit=yes [Install] WantedBy=default.target
- Run the following commands to start the run-script-when-shutdown service:
systemctl enable run-script-when-shutdown systemctl start run-script-when-shutdownNote
- You can configure the run-script-when-shutdown service to specify the script to run. This allows you to flexibly change the script to run.
- If the run-script-when-shutdown service is no longer needed, run the
systemctl disable run-script-when-shutdowncommand to disable the service.