All Products
Search
Document Center

Elastic Compute Service:Keep services alive with a Cloud Assistant plugin

Last Updated:May 08, 2026

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.

Note

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.sh

  • Python 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.sh

    • Python program: python /home/root/main.py

  • Replace <user_name> with the user that starts the service. Run cut -d: -f1 /etc/passwd to view existing users.

  • Replace <group_name> with the user's group name. Run cut -d: -f1 /etc/group to view existing user groups.

Warning

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 13

Disable 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.

Note

Disabling keepalive terminates the service process, removes the auto-start configuration, and deletes the service configuration generated by the ecs-tool-servicekeepalive plugin.

Example

  1. 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
  2. (Optional) Run the following command to query the status of the service:

    ps aux | grep test-keepalive.sh

    The 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.sh
  3. 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 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
  4. Query the keepalive status and the service status.

    1. 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
    2. (Optional) Run the following command to query the status of the service:

      ps aux | grep test-keepalive.sh

      The 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
  5. (Optional) Verify the keepalive effect.

    1. 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.sh process. Replace <PID> with the process ID obtained from the ps command.

      sudo date && kill -9 <PID>
    2. (Optional) Run the following command to query the status of the service:

      ps aux | grep test-keepalive.sh

      The 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
  6. 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

References

See Cloud Assistant overview.