init.sh
Overview
`init.sh` is a Bash shell script designed to manage the lifecycle of a Dogecoin daemon process (`dogecoind`). Its primary purpose is to start the Dogecoin node with specific runtime parameters and ensure proper shutdown handling upon receiving termination signals. The script encapsulates the startup and graceful termination logic, making it suitable for initializing Dogecoin nodes in containerized environments or as part of automated deployment workflows.
Detailed Explanation
Script Behavior and Flow
Set Bash Options
set -eCauses the script to exit immediately if any command exits with a non-zero status, ensuring early failure detection.
Function:
start_coinstart_coin() { dogecoind \ -port=22556 \ -rpcuser=user \ -rpcpassword=password \ -rpcallowip=0.0.0.0/0 \ -rpcbind=0.0.0.0 \ -datadir=/data \ -printtoconsole=1 \ -server=1 \ -nolisten=1 \ -txindex=1 \ -disablewallet=1 \ -zmqpubhashtx=tcp://127.0.0.1:28332 \ -zmqpubhashblock=tcp://127.0.0.1:28332 \ -rpcworkqueue=1100 \ -rpcport=8332 \ -maxmempool=2000 \ -dbcache=4000 & PID="$!" }Purpose: Starts the Dogecoin daemon (
dogecoind) with a predefined set of parameters.Parameters passed to
dogecoind:-port=22556: Listen port for P2P connections (default Dogecoin port).-rpcuser=userand-rpcpassword=password: Basic authentication for RPC interface.-rpcallowip=0.0.0.0/0: Allow RPC connections from any IP.-rpcbind=0.0.0.0: Bind RPC server on all network interfaces.-datadir=/data: Use/datadirectory for blockchain data.-printtoconsole=1: Print logs to console (stdout).-server=1: Enable server mode for RPC commands.-nolisten=1: Do not accept P2P connections (node is non-listening).-txindex=1: Maintain an index of all transactions, enabling richer queries.-disablewallet=1: Disable wallet functionality (node is running in a wallet-less mode).-zmqpubhashtxand-zmqpubhashblock: Publish transaction and block hashes over ZMQ at TCP port 28332.-rpcworkqueue=1100: Set RPC work queue size.-rpcport=8332: Listen RPC server on port 8332.-maxmempool=2000: Set max mempool size to 2000 MB.-dbcache=4000: Set database cache size to 4000 MB.
Execution: Runs the command in the background (
&), and captures the background process PID in variablePID.
Function:
stop_coinstop_coin() { echo "Catching signal and sending to PID: $PID" kill $PID while $(kill -0 $PID 2>/dev/null); do sleep 1 done }Purpose: Gracefully stops the Dogecoin daemon process.
Behavior:
Prints a message indicating the signal catch and targeted PID.
Sends a
SIGTERMsignal to the process withkill $PID.Enters a loop to check if the process is still alive using
kill -0 $PID. This does not send a signal but checks for process existence.Sleeps 1 second between checks until the process exits, ensuring a clean shutdown.
Signal Trap Setup
trap 'stop_coin' TERM INTSets up signal handlers for
TERMandINTsignals.When the script receives termination (
SIGTERM) or interrupt (SIGINT) signals, it runs thestop_coinfunction to stop the Dogecoin daemon gracefully.
Main Execution Flow
start_coin wait $PIDCalls
start_cointo launch the Dogecoin daemon.wait $PIDsuspends the script execution until the Dogecoin process terminates, ensuring the script stays alive as long as the daemon is running.
Usage Examples
Run directly in a shell:
./init.shThis will start the Dogecoin daemon with the specified options, keep the process running, and handle termination signals gracefully.
Run in a Docker container entrypoint:
The script can serve as an entrypoint script in a Docker container that runs a Dogecoin node.
Important Implementation Details
Graceful Shutdown:
The script usestrapto catch termination signals and callsstop_cointo kill the daemon and wait until it fully exits. This prevents orphaned processes and potential data corruption.Background Process Management:
The Dogecoin daemon is launched in the background, with its PID captured immediately to allow controlled termination.Use of
set -e:
Ensures the script exits on any error, providing robustness in automated environments.ZMQ Publisher:
The daemon is configured to publish transaction and block hash notifications over ZeroMQ on localhost, which can be consumed by other services for real-time blockchain event processing.RPC Configuration:
The node exposes an RPC interface on all network interfaces, with a fixed username and password. This is convenient for testing or controlled environments but should be secured in production.
Interaction with Other System Components
Dogecoin Daemon (
dogecoind):
This script is a wrapper around thedogecoindexecutable, managing its lifecycle.Data Directory (
/data):
The blockchain data is stored in/data; the script expects this directory to be mounted or available, commonly used in container setups.ZeroMQ Subscribers:
Other components or services can subscribe to the ZMQ ports to receive live notifications about new blocks and transactions.RPC Clients:
External clients or services can interact with the Dogecoin node via RPC on port 8332 using the configured credentials.
Diagram: Workflow of init.sh
flowchart TD
A[Start Script] --> B[start_coin function]
B --> C[dogecoind process starts in background]
C --> D[PID stored in $PID]
D --> E[wait $PID - script waits for dogecoind to finish]
subgraph Signal Handling
F[Receive SIGTERM or SIGINT] --> G[trap calls stop_coin]
G --> H[stop_coin sends SIGTERM to $PID]
H --> I[Loop until dogecoind process exits]
I --> J[dogecoind terminates]
J --> E
end
Summary
This script is a lightweight, robust entrypoint utility designed to start and manage a Dogecoin node daemon with a specific set of configurations. It ensures proper startup, exposes interfaces for RPC and ZeroMQ notification, and handles termination signals gracefully to avoid data corruption or orphaned processes. It is particularly suited for containerized environments where controlled start/stop of the node process is necessary.