# How Can I Use systemd to Manage the Hybrid Deployment Agent?

## Question

How can I use systemd to manage the Hybrid Deployment Agent?

## Environment

Container platform: **Docker** and **Podman**

## Answer

You can configure `systemd` to manage the Hybrid Deployment Agent and start it automatically when the host boots.

> **Note:** These instructions assume a rootless Docker or Podman installation. The `systemd` service runs as the specified user, not as the root user.

1. Stop the Hybrid Deployment Agent. Depending on the container runtime, run one of the following commands:

   - **Docker**

     ```bash
     ./hdagent.sh [-r docker] stop

     ```
   - **Podman**

     ```bash
     ./hdagent.sh [-r podman] stop

     ```
2. Create the folder path and open the user `systemd` unit file for editing:

   ```bash
   mkdir -p ~/.config/systemd/user/
   vi ~/.config/systemd/user/hdagent.service

   ```
3. Add the appropriate configuration to the `hdagent.service` file.

   - **Docker**

     ```text
       [Unit]
       Description=Fivetran Hybrid Deployment Agent
       After=network.target docker.service
       Requires=default.target
       
       [Service]
       Type=oneshot
       RemainAfterExit=true
       WorkingDirectory=%h/fivetran
       ExecStart=/bin/bash %h/fivetran/hdagent.sh start
       ExecStop=/bin/bash %h/fivetran/hdagent.sh stop
       ExecStop=%h/fivetran/hdagent.sh stop
       Environment=PATH=/usr/bin:/bin
       
       [Install]
       WantedBy=default.target

     ```
   - **Podman**

     ```text
       [Unit]
       Description=Fivetran Hybrid Deployment Agent
       After=network.target podman.service
       Requires=default.target
       
       [Service]
       Type=oneshot
       RemainAfterExit=true
       WorkingDirectory=%h/fivetran
       ExecStart=/bin/bash %h/fivetran/hdagent.sh start
       ExecStop=/bin/bash %h/fivetran/hdagent.sh stop
       Environment=PATH=/usr/bin:/bin
       
       [Install]
       WantedBy=default.target

     ```
4. Reload and enable the service:

   ```bash
   export XDG_RUNTIME_DIR=/run/user/$(id -u)
   systemctl --user daemon-reload
   systemctl --user enable hdagent.service
   systemctl --user start hdagent.service
   systemctl --user status hdagent.service

   ```
5. Verify that the agent is running:

   - **Docker**

   ```bash
   docker ps -a
   docker logs <container_name_or_id>

   ```

   - **Podman**

   ```bash
   podman ps -a
   podman logs <container_name_or_id>

   ```
6. Enable user lingering so that the user `systemd` instance starts automatically after the host boots. Replace $`USER` with the Unix user that runs the agent.

   ```bash
   sudo loginctl enable-linger $USER

   ```
7. Verify that user lingering is enabled:

   ```bash
   loginctl show-user $USER

   ```
8. Confirm that the `Linger` property is set to `yes`.
