A service or script can stop due to application errors, instance restarts, or power outages, which can lead to business disruption. This topic describes how to use the Cloud Assistantecs-tool-servicekeepalive plugin to ensure keepalive for a service.
How it works
The Cloud Assistant ecs-tool-servicekeepalive plugin uses systemd, the Linux service manager, to automatically restart an interrupted service or script, ensuring service reliability and continuity. To use the plugin for keepalive, provide a startup command. The plugin then generates the required systemd service configuration, starts the service, and enables auto-start on boot. No manual file editing is necessary.
systemd is a service manager in Linux that can perform tasks such as enabling auto-start on boot and restarting services that stop unexpectedly. For more information, see the official systemd documentation.
Procedure
Enable service keepalive
After you deploy your service, run the Cloud Assistant pluginecs-tool-servicekeepalive as the root user.
Run as root
sudo acs-plugin-manager --exec --plugin ecs-tool-servicekeepalive --params "start,'<cmd>'"
<cmd>: The startup command for the service. See the examples below.
-
Shell script: (
/bin/bash /home/ecs-user/keepalive-simple/test-keepalive.sh). -
Python script: (
python /home/root/main.py).
Run as specified user
sudo acs-plugin-manager --exec --plugin ecs-tool-servicekeepalive --params "start,execstart='<cmd>',user=<user_name>,group=<group_name>"
-
<cmd>: The startup command for the service. See the examples below.
-
Shell script: (
/bin/bash /home/ecs-user/keepalive-simple/test-keepalive.sh). -
Python script: (
python /home/root/main.py).
-
-
<user_name>: The username that runs the service. Run
cut -d: -f1 /etc/passwdto view existing users. -
<group_name>: The group name for the service. Run
cut -d: -f1 /etc/groupto view existing user groups.
-
You must use an absolute path for the script or program file.
-
If enabling keepalive fails, first query the keepalive status of the service and then disable keepalive for the service for the failed entry before you try again. This prevents multiple service processes from starting, which can cause issues with your application.
Query keepalive status
Run the following command to check the keepalive status.
sudo acs-plugin-manager --exec --plugin ecs-tool-servicekeepalive --params "status"
The following example output indicates that keepalive is enabled.
service_name execstart user group status
ecs_keepalive_1744262359.service /bin/bash /home/ecs-user/keepalive-simple/test-keepalive.sh /home/ecs-user/keepalive-simple/test-keepalive.log active (running) since Thu 2025-04-10 13
Disable service keepalive
If you need to disable keepalive, run the following command.
sudo acs-plugin-manager --exec --local --plugin ecs-tool-servicekeepalive --params "stop <service_name>"
<service_name>: The name of the service configuration. This value is in the service_name column of the output from the Query keepalive status step.
When you disable keepalive, the service process is terminated, the auto-start configuration is removed, and the service configuration file created by the ecs-tool-servicekeepalive plugin is deleted.
Example
-
Prepare a test service script.
This script uses
/home/ecs-useras the working directory. Replace it with your actual working directory.# Create the keepalive-simple directory in the /home/ecs-user directory and create the test-keepalive.sh script in the directory. sudo mkdir -p /home/ecs-user/keepalive-simple && \ sudo tee /home/ecs-user/keepalive-simple/test-keepalive.sh > /dev/null << 'EOF' #!/bin/bash # Write one log line per second to the log file that you specify. while true do sudo echo "$(date '+%Y-%m-%d %H:%M:%S') progress is alive" >> $1 sleep 1 done EOF # Grant execute permissions to the script. sudo chmod +x /home/ecs-user/keepalive-simple/test-keepalive.sh -
(Optional) Query the service running status.
Run the following command to query the service running status.
ps aux | grep test-keepalive.shThe following example output indicates that the service is not running.
ecs-user 2207 0.0 0.0 221528 916 pts/0 S+ 11:34 0:00 grep --color=auto test-keepalive.sh -
Enable keepalive for the service.
sudo acs-plugin-manager --exec --plugin ecs-tool-servicekeepalive --params "start,'/bin/bash /home/ecs-user/keepalive-simple/test-keepalive.sh /home/ecs-user/keepalive-simple/test-keepalive.log'"The following example output indicates that keepalive is enabled.
Created symlink /etc/systemd/system/multi-user.target.wants/ecs_keepalive_1744256544.service → /etc/systemd/system/ecs_keepalive_1744256544.service. Start systemd service for "/bin/bash /home/ecs-user/keepalive-simple/test-keepalive.sh /home/ecs-user/keepalive-simple/test-keepalive.log" success -
Query the keepalive status and service running status.
-
Run the following command to query the keepalive status.
sudo acs-plugin-manager --exec --plugin ecs-tool-servicekeepalive --params "status"The following example output indicates that keepalive is enabled and the service is running.
service_name execstart user group status ecs_keepalive_1744256544.service /bin/bash /home/ecs-user/keepalive-simple/test-keepalive.sh /home/ecs-user/keepalive-simple/test-keepalive.log active (running) since Thu 2025-04-10 11 -
Run the following command to query the service running status.
ps aux | grep test-keepalive.shThe following example output indicates that the service is running.
root 3144 0.0 0.0 222200 3420 ? Ss 11:42 0:00 /bin/bash /home/ecs-user/keepalive-simple/test-keepalive.sh /home/ecs-user/keepalive-simple/test-keepalive.log ecs-user 6841 0.0 0.0 221660 968 pts/0 S+ 11:49 0:00 grep --color=auto test-keepalive.sh
-
-
(Optional) Verify keepalive.
-
Manually stop the service to verify that Cloud Assistant automatically restarts its process.
Method 1: Restart ECS instance
Restart the instance to simulate an unexpected instance restart.
Method 2: Terminate the process
Run the following command to terminate the
test-keepalive.shprocess. Replace <PID> with the PID of the program that is returned by the ps command.sudo date && kill -9 <PID> -
Run the following command to query the service running status.
ps aux | grep test-keepalive.shThe following example output indicates that the service is running.
root 33061 0.0 0.0 222200 3504 ? Ss 13:19 0:00 /bin/bash /home/ecs-user/keepalive-simple/test-keepalive.sh /home/ecs-user/keepalive-simple/test-keepalive.log ecs-user 34558 0.0 0.0 221660 2556 pts/0 S+ 13:23 0:00 grep --color=auto test-keepalive.sh
-
-
Disable keepalive for the service.
Run the following command to disable keepalive.
sudo acs-plugin-manager --exec --local --plugin ecs-tool-servicekeepalive --params "stop ecs_keepalive_1744256544.service"The following example output indicates that the keepalive configuration is disabled.
service check ok, file:ecs_keepalive_1744256544.service is valid Removed /etc/systemd/system/multi-user.target.wants/ecs_keepalive_1744256544.service. stop service ok, service:ecs_keepalive_1744256544.service is stopped and removed
References
For more information about Cloud Assistant, see Cloud Assistant overview.