init.sh


Overview

`init.sh` is a shell script designed to initialize and manage the lifecycle of a PostgreSQL database instance running inside a Docker container. It provides a controlled environment for starting the PostgreSQL server with specific configuration parameters and ensures graceful shutdown upon receiving termination signals.

The script achieves this by:

This script is typically used as an entrypoint or initialization script in containerized environments where robust service start/stop behavior is critical.


File Components

1. start_service()

Purpose

Starts the PostgreSQL server with specific configuration overrides and runs it in the background.

Implementation Details

Parameters

Returns

Usage Example

start_service
echo "PostgreSQL started with PID $PID"

2. stop_service()

Purpose

Gracefully stops the PostgreSQL server process by sending it a termination signal, then waits until the process has fully exited.

Implementation Details

Parameters

Returns

Usage Example

This function is intended to be called via signal trapping, but can be manually invoked as:

stop_service
echo "PostgreSQL stopped"

3. Signal Trapping

trap 'stop_service' TERM INT

Purpose

Sets up traps for the `TERM` (termination) and `INT` (interrupt, e.g., Ctrl+C) signals. When either signal is received, it runs the `stop_service` function.

This ensures the PostgreSQL server is cleanly shut down when the container or host system requests termination.


4. Script Execution Flow

  1. start_service is called to launch PostgreSQL in the background.

  2. The script waits on the PostgreSQL process using wait $PID.

  3. If a termination signal is caught, stop_service is invoked to shut down PostgreSQL gracefully.


Important Implementation Details


Interaction with Other System Components

This script is a crucial part of the container startup routine, ensuring the database is launched with desired configuration parameters and stopped cleanly to maintain data integrity.


Visual Diagram

Below is a flowchart representing the main functions and control flow of `init.sh`:

flowchart TD
    Start["Start Script"] --> SetTrap["Set trap for TERM and INT signals"]
    SetTrap --> StartService["Call start_service()"]
    StartService -->|Runs postgres in background| CapturePID["Capture PID of postgres process"]
    CapturePID --> WaitProcess["wait $PID"]

    %% Signal handling flow
    subgraph SignalHandling["On TERM or INT signal"]
        SignalCaught["Signal received"] --> CallStopService["Call stop_service()"]
        CallStopService --> SendKill["kill $PID (SIGTERM)"]
        SendKill --> LoopCheck["while kill -0 $PID"]
        LoopCheck --> Sleep["sleep 1"]
        Sleep --> LoopCheck
        LoopCheck --> ExitStop["Postgres stopped"]
    end

    WaitProcess -->|Signal interrupts wait| SignalCaught

Summary

`init.sh` is a streamlined shell script that acts as a robust initializer and shutdown controller for a PostgreSQL service running in a Docker container. It configures specific PostgreSQL runtime parameters, manages the lifecycle of the database process, and ensures graceful shutdown by trapping termination signals and forwarding them to the database process. This behavior helps maintain database stability and data integrity in containerized environments.


Appendix: Usage in Container Context

Typically, this script would be specified as the entrypoint or command in a Dockerfile or Docker Compose configuration:

COPY init.sh /usr/local/bin/init.sh
ENTRYPOINT ["/usr/local/bin/init.sh"]

Or in a Docker Compose file:

services:
  db:
    image: postgres:latest
    entrypoint: /usr/local/bin/init.sh
    environment:
      POSTGRES_PASSWORD: example

This ensures that the container runs PostgreSQL with the specified parameters and handles termination signals gracefully, enabling smooth restarts and shutdowns.