init.sh


Overview

`init.sh` is a shell script designed to initialize and manage the lifecycle of the `thornode` daemon process within a container or server environment. Its primary purpose is to start the `thornode` service with specific network and RPC configurations, handle termination signals gracefully, and ensure clean shutdown of the process.

This script enables automated and robust management of the `thornode` node by:


Detailed Explanation

Shebang and Shell Settings

#!/bin/sh
set -e

Installing Bash

apk add bash

Functions

start_coin

Starts the `thornode` daemon process with specific network and RPC settings.

start_coin() {
  thornode start \
    --p2p.pex=false \
    --p2p.laddr=tcp://0.0.0.0:27146 \
    --proxy_app=tcp://127.0.0.1:27148 \
    --rpc.laddr=tcp://0.0.0.0:27147 &
  PID="$!"
}

**Usage example**:

start_coin
echo "Thornode started with PID $PID"

stop_coin

Terminates the running `thornode` daemon gracefully.

stop_coin() {
  echo "Catching signal and sending to PID: $PID"
  kill $PID
  while $(kill -0 $PID 2>/dev/null); do
    sleep 1
  done
}

**Usage example**:

stop_coin
echo "Thornode has been stopped"

Signal Trapping

trap 'stop_coin' TERM INT

Main Execution

start_coin
wait $PID

Implementation Details and Algorithms


Interaction with Other Parts of the System


Summary

`init.sh` is a lightweight, robust startup script for managing the `thornode` daemon process, ensuring it starts with correct parameters, handles system signals gracefully, and exits cleanly. Its design supports reliable operation in environments such as Docker containers or dedicated servers.


Visual Diagram: Workflow of init.sh

flowchart TD
    A[Start script] --> B[Install bash via apk]
    B --> C[start_coin function]
    C --> D[Launch thornode with flags in background]
    D --> E[Store thornode PID]
    E --> F[Set trap for TERM and INT signals]
    F --> G[Wait for thornode process to exit]

    subgraph Signal Handling
        H[Receive TERM or INT signal] --> I[Invoke stop_coin function]
        I --> J[Send SIGTERM to thornode PID]
        J --> K[Poll thornode process status]
        K --> L{Is process still running?}
        L -- Yes --> K
        L -- No --> M[thornode stopped gracefully]
    end

    G --> |Signal received| H

End of Documentation for init.sh