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:
Installing necessary shell dependencies.
Starting the
thornodeprocess with predefined parameters.Capturing the process ID (PID) for later control.
Handling termination signals (
TERM,INT) to shut down the daemon cleanly.Waiting on the daemon process to finish before exiting.
Detailed Explanation
Shebang and Shell Settings
#!/bin/sh
set -e
#!/bin/shspecifies that the script runs under the Bourne shell or compatible shell.set -einstructs the shell to exit immediately if any command exits with a non-zero status, ensuring failures are caught early.
Installing Bash
apk add bash
Uses Alpine Linux’s package manager
apkto installbash.Necessary to ensure Bash is available since the script may rely on Bash features or the environment requires it.
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="$!"
}
Purpose: Launch
thornodein the background with particular flags.Parameters: None.
Returns: None explicitly; however, it sets the global variable
PIDto the process ID of the started daemon.Flags explained:
--p2p.pex=false: Disables peer exchange (PEX) in the peer-to-peer network.--p2p.laddr=tcp://0.0.0.0:27146: Listens on all network interfaces at TCP port 27146 for P2P connections.--proxy_app=tcp://127.0.0.1:27148: Connects to the proxy application on localhost at port 27148.--rpc.laddr=tcp://0.0.0.0:27147: Listens on all network interfaces at TCP port 27147 for RPC calls.
&runs the command in the background.PID="$!"stores the PID of the last background process started.
**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
}
Purpose: Sends a termination signal to the
thornodeprocess and waits for it to exit.Parameters: None.
Returns: None.
How it works:
Prints a message indicating it is sending a kill signal.
kill $PIDsends the defaultSIGTERMto the process.The
whileloop polls the process status usingkill -0 $PID(which does not send a signal but checks if the process exists).The loop sleeps in 1-second intervals until the process no longer exists (i.e., has exited).
**Usage example**:
stop_coin
echo "Thornode has been stopped"
Signal Trapping
trap 'stop_coin' TERM INT
Sets up OS signal traps to catch
TERM(termination) andINT(interrupt, e.g., Ctrl+C) signals.When such a signal is received, the
stop_coinfunction is invoked to gracefully shut down the daemon.
Main Execution
start_coin
wait $PID
Starts the
thornodeprocess.wait $PIDpauses the script’s execution until thethornodeprocess exits.This ensures the script remains active to handle signals and to prevent the container or session from terminating prematurely.
Implementation Details and Algorithms
The script uses simple process management techniques common in Unix shell scripting.
It leverages signal trapping to ensure the daemon is not abruptly killed but is terminated cleanly.
The
whileloop instop_coinuses thekill -0technique to poll process existence without sending termination signals repeatedly.Background execution and PID tracking allow asynchronous process control while maintaining script flow.
Interaction with Other Parts of the System
thornode: This script directly launches and manages the lifecycle of the
thornodeprogram, which is presumably the core node daemon of the system.Proxy app: The
thornodeprocess connects to a proxy application on localhost at port 27148, indicating interaction with another local service or component.Network: The script configures the network interfaces and ports used by
thornodefor P2P communication and RPC, critical for node operation and external communication.Container or VM environment: Likely runs in a containerized or isolated environment where this script serves as the entrypoint or initialization routine.
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