bm-rotate.sh

Overview

bm-rotate.sh is a bash script designed to send a SIGHUP (signal hang up) to a running Docker container process associated with the "block_manager" service. Its primary functionality is to identify the container by a specific Docker Compose service label, retrieve the process ID (PID) of the container's main process, check if the process is active, and then send the SIGHUP signal to prompt the container to reload its configuration or perform any other signal-defined behavior. The script provides timestamped log messages indicating success or failure of each operation.

Script Workflow and Functionality

The script performs the following key steps:

  1. Locate the Block Manager container
    Uses docker ps with a filter on the label com.docker.compose.service=block_manager to find the container ID of the relevant container. This label is typically assigned by Docker Compose to containers based on the service name.

  2. Check if the container exists
    The script verifies whether container_id is non-empty. If empty, it logs that the container was not found.

  3. Retrieve the container's main process ID (PID)
    Uses docker inspect with a Go template to extract the PID of the container's init process on the host system.

  4. Check if the process is running
    Uses ps -e and grep to verify if the process with the retrieved PID is active.

  5. Send SIGHUP to the running process
    If the process exists, sends the SIGHUP signal using kill -SIGHUP to prompt the container to reload.

  6. Logging with timestamps
    Each outcome (success or failure) logs a message with the current ISO-8601 timestamp using date -Is.

Important Implementation Details

Detailed Command Descriptions

Command

Purpose

sudo docker ps -q -a --filter "label=..."

Lists all containers (running or stopped) matching the label, outputs container IDs only (-q).

sudo docker inspect --format '{{.State.Pid}}'

Extracts the PID of the container's process on the host.

`ps -e

grep -q -e $pid`

sudo kill -SIGHUP $pid

Sends the SIGHUP signal to the container's process.

echo "[$(date -Is)] ..."

Logs actions with ISO-8601 timestamps.

Usage Example

./bm-rotate.sh

The script can be executed manually or integrated into automation workflows where sending a SIGHUP to the block manager container is required, for example, after configuration changes.

Interaction with Other System Components

This script is intended to be part of operational scripts managing container lifecycle or signaling for configuration reloads without container restarts.


Mermaid Flowchart Diagram

flowchart TD
A[Start] --> B{Find container ID}
B -->|Found| C[Get container PID]
B -->|Not Found| G[Log "Couldn't find Block Manager container"]
C --> D{Is PID process running?}
D -->|Yes| E[Send SIGHUP to PID]
D -->|No| F[Log "Couldn't find Block Manager container process"]
E --> H[Log "SIGHUP sent"]
F --> I[End]
G --> I
H --> I

This flowchart illustrates the decision points and main actions within the script, highlighting the conditional checks on container and process existence followed by signaling and logging steps.