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:
Locate the Block Manager container
Usesdocker pswith 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.Check if the container exists
The script verifies whethercontainer_idis non-empty. If empty, it logs that the container was not found.Retrieve the container's main process ID (PID)
Usesdocker inspectwith a Go template to extract the PID of the container's init process on the host system.Check if the process is running
Usesps -eandgrepto verify if the process with the retrieved PID is active.Send SIGHUP to the running process
If the process exists, sends the SIGHUP signal usingkill -SIGHUPto prompt the container to reload.Logging with timestamps
Each outcome (success or failure) logs a message with the current ISO-8601 timestamp usingdate -Is.
Important Implementation Details
The script runs with
set -euwhich ensures it exits on any unset variables (-u) or errors (-e), improving robustness.It requires
sudoprivileges for Docker commands and to send signals to processes, implying it should be run by a user with appropriate permissions.The search for the container is based on Docker Compose service labeling, making it specific to environments using Docker Compose with this label convention.
The use of
ps -e | grep -q -e $pidchecks for process existence but might incorrectly match substrings; however, this is unlikely for numeric PIDs and is a common lightweight check.
Detailed Command Descriptions
Command | Purpose |
|---|---|
| Lists all containers (running or stopped) matching the label, outputs container IDs only ( |
| Extracts the PID of the container's process on the host. |
`ps -e | grep -q -e $pid` |
| Sends the SIGHUP signal to the container's process. |
| 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
Docker Engine: Utilizes Docker CLI commands (
docker ps,docker inspect) to interact with container metadata and state.Block Manager Service: Targets the container running the
block_managerservice (identified by Docker Compose label).Host OS Process Management: Uses process signals (
kill -SIGHUP) and process listing (ps) to control container processes at the OS level.
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.