init.sh


Overview

`init.sh` is a Bash shell script designed to manage the lifecycle of a Bitcoin daemon process (`bitcoind`). Its primary purpose is to start the `bitcoind` node with a specific configuration tailored for a headless, server-oriented environment, and to gracefully stop the process upon receiving termination signals.

Key functionalities include:

This script is typically used in containerized or server environments where automated startup and controlled shutdown of the Bitcoin node are required.


Detailed Explanation

Functions

start_coin()

**Purpose:** Starts the `bitcoind` daemon process with predefined configuration flags and runs it in the background.

**Implementation Details:**

**Parameters:** None.

**Returns:** None (side effect: sets global `PID`).

**Usage Example:**

start_coin
echo "Bitcoin daemon started with PID $PID"

stop_coin()

**Purpose:** Gracefully stops the running `bitcoind` daemon by sending a kill signal to its process and waiting for it to exit.

**Implementation Details:**

**Parameters:** None.

**Returns:** None.

**Usage Example:**

stop_coin
echo "Bitcoin daemon stopped"

Signal Trap

**Purpose:** Sets up signal handlers to catch termination signals (`TERM` and `INT`) and invoke `stop_coin()` for clean shutdown.

**Implementation Details:**


Main Execution Flow

  1. Calls start_coin to launch the Bitcoin daemon in the background.

  2. Waits for the daemon process to finish by calling wait $PID.

  3. The script effectively runs until the Bitcoin daemon exits or a termination signal is received.


Important Implementation Details and Algorithms


Interaction With Other Components

This script is often used as an entrypoint script in Docker containers or as a startup service script within server environments.


Visual Diagram

flowchart TD
    StartCoin["start_coin()"]
    StopCoin["stop_coin()"]
    TrapSignals["trap 'stop_coin' TERM INT"]
    WaitPID["wait $PID"]
    Bitcoind["bitcoind daemon"]

    StartCoin -->|starts| Bitcoind
    Bitcoind -->|runs with PID stored in $PID| WaitPID
    TrapSignals -->|on SIGTERM or SIGINT| StopCoin
    StopCoin -->|kill $PID and wait| Bitcoind
    WaitPID -->|blocks until Bitcoind exits| End["Script exits"]

Summary

`init.sh` is a lightweight and robust initialization script for running and managing a Bitcoin daemon in a controlled environment. It encapsulates the startup configuration, signal handling for graceful shutdown, and process monitoring in a few lines of Bash code. This script is suitable for deployment scenarios where automated and reliable Bitcoin node management is required, such as containerized deployments or dedicated server setups.