Configure the ecs-tool-servicekeepalive Cloud Assistant plugin to auto-restart services or scripts that stop due to exceptions, instance restarts, or power outages.
How it works
The ecs-tool-servicekeepalive plugin leverages Linux systemd to periodically monitor and auto-restart interrupted services or scripts. You provide a startup command, and the plugin generates the systemd service configuration automatically. systemd then starts the service and enables it on system startup.
systemd is a Linux service manager that can start services on boot and restart them after unexpected stops. See systemd documentation.
Procedure
Enable keepalive for a service
After you deploy services on an ECS instance, start the ecs-tool-servicekeepalive plugin as the root user.
Start a service as the root user
sudo acs-plugin-manager --exec --plugin ecs-tool-servicekeepalive --params "start,'<cmd>'"Replace <cmd> with the startup command. Examples:
Shell program:
/bin/bash /home/ecs-user/keepalive-simple/test-keepalive.shPython program:
python /home/root/main.py
Start a service as a specific user
sudo acs-plugin-manager --exec --plugin ecs-tool-servicekeepalive --params "start,execstart='<cmd>',user=<user_name>,group=<group_name>"Replace <cmd> with the startup command. Examples:
Shell program:
/bin/bash /home/ecs-user/keepalive-simple/test-keepalive.shPython program:
python /home/root/main.py
Replace <user_name> with the user that starts the service. Run
cut -d: -f1 /etc/passwdto view existing users.Replace <group_name> with the user's group name. Run
cut -d: -f1 /etc/groupto view existing user groups.
You must specify the absolute path of the script or program.
If you cannot enable keepalive for a service, perform the following operations to prevent duplicate processes: Query the keepalive status of the service, disable keepalive for the service, resolve the issue, restart the service, and then re-enable keepalive.
Query the keepalive status of a service
Check whether keepalive is enabled for a service:
sudo acs-plugin-manager --exec --plugin ecs-tool-servicekeepalive --params "status"The following 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 13Disable keepalive for a service
Disable keepalive for a service:
sudo acs-plugin-manager --exec --local --plugin ecs-tool-servicekeepalive --params "stop <service_name>"Replace <service_name> with the actual service name from the service_name column in the output of Query the keepalive status of a service.
Disabling keepalive terminates the service process, removes the auto-start configuration, and deletes the service configuration generated by the ecs-tool-servicekeepalive plugin.
Example
Prepare a script for a test service.
In this script, the working directory is
/home/ecs-user. Replace it with your actual working directory.# In the /home/ecs-user directory, create the keepalive-simple folder and create the test-keepalive.sh script in the folder. sudo mkdir -p /home/ecs-user/keepalive-simple && \ sudo tee /home/ecs-user/keepalive-simple/test-keepalive.sh > /dev/null << 'EOF' #!/bin/bash # Generate a log message every second in the specified log file. while true do sudo echo "$(date '+%Y-%m-%d %H:%M:%S') progress is alive" >> $1 sleep 1 done EOF # Grant executable permissions on the script. sudo chmod +x /home/ecs-user/keepalive-simple/test-keepalive.sh(Optional) Run the following command to query the status of the service:
ps aux | grep test-keepalive.shThe following output indicates that the service is not started:
ecs-user 2207 0.0 0.0 221528 916 pts/0 S+ 11:34 0:00 grep --color=auto test-keepalive.shEnable 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 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" successQuery the keepalive status and the service status.
Query the keepalive status:
sudo acs-plugin-manager --exec --plugin ecs-tool-servicekeepalive --params "status"The following 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(Optional) Run the following command to query the status of the service:
ps aux | grep test-keepalive.shThe following 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 the keepalive effect.
Manually stop the service process to verify that Cloud Assistant restarts it.
Method 1: Restart the ECS instance
Restart the instance to simulate an unexpected restart.
Method 2: Terminate the service process
Terminate the
test-keepalive.shprocess. Replace <PID> with the process ID obtained from the ps command.sudo date && kill -9 <PID>(Optional) Run the following command to query the status of the service:
ps aux | grep test-keepalive.shThe following 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:
sudo acs-plugin-manager --exec --local --plugin ecs-tool-servicekeepalive --params "stop ecs_keepalive_1744256544.service"The following output indicates that keepalive 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