startup.sh
Overview
`startup.sh` is a lightweight shell script designed to verify if a daemon (likely an Ethereum client or similar JSON-RPC server) is actively listening for network connections on the local machine. This script primarily serves as a startup probe to check the readiness of the daemon by querying its JSON-RPC endpoint. It helps orchestrators or monitoring systems determine if the service is operational before proceeding with dependent processes.
The script also supports disabling the startup probe check via the presence of a specific file, allowing manual or automated override of the readiness detection.
Detailed Explanation
Script Workflow
Disable Probe Check (DISABLE_STARTUP_PROBE)
The script first checks for the existence of the file/data/disable_startup. If this file exists, the startup probe is disabled, the script outputs a message, and exits successfully (exit 0). This mechanism provides a simple toggle to bypass the readiness check.Check Daemon Listening Status (
net_listening)
If the probe is not disabled, the script sends a JSON-RPC request tohttp://localhost:8545, invoking thenet_listeningmethod, which returns whether the daemon is currently listening on the network.Parse and React to Response
If the curl command fails (e.g., daemon not reachable), the script exits with status 1 indicating failure.
If the response indicates the daemon is listening (
true), the script prints confirmation and exits with status 0.Otherwise, it prints that the daemon is not listening and exits with status 1.
Variables and Constants
Variable | Description |
|---|---|
Path to the file that disables the startup check if present (`/data/disable_startup`). | |
`NET_LISTENING` | Stores the JSON-RPC response from the daemon querying `net_listening`. |
`LISTENING` | Parsed boolean value extracted from `NET_LISTENING` indicating if daemon is listening. |
Commands and Tools Used
curl
Used to send an HTTP POST request with a JSON payload to the daemon's JSON-RPC endpoint.jq
A command-line JSON processor used to parse the JSON response and extract the.resultfield.bashconditional expressions ([[ ]])
Used to test file existence and string equality.
Usage Example
# Run the startup probe script
./startup.sh
# Expected outputs:
# If /data/disable_startup exists
# startup probe disabled
# If daemon is listening
# daemon is listening
# If daemon is not listening or unreachable
# daemon is not listening
This script is typically used as a readiness probe in container orchestration platforms (e.g., Kubernetes) or in system init scripts to ensure that the daemon is ready before starting dependent services.
Important Implementation Details
Probe Disabling Mechanism: The simple presence check of
/data/disable_startupallows operators or automation scripts to disable the probe without modifying the script or deployment configuration.Robustness:
The script gracefully exits with status1if the daemon is unreachable or the curl command fails, which helps orchestrators detect failure states.Lightweight and Dependency Minimal:
The script relies only on standard Unix tools (bash,curl,jq), ensuring portability and ease of integration.JSON-RPC Interaction:
The script uses a minimal JSON-RPC payload to query daemon status, making it adaptable to any JSON-RPC compatible server listening on port 8545.
Interaction with Other System Components
Daemon (e.g., Ethereum client)
The script queries the daemon's JSON-RPC interface onlocalhost:8545. It assumes the daemon exposes anet_listeningmethod that returns a boolean indicating network listening status.Orchestration System / Monitoring Tools
This script is intended to be used as a readiness/startup probe in container orchestrators (Kubernetes, Docker Swarm) or system supervisors to gate service startup or signal service health.Configuration / Data Volume
The presence of/data/disable_startupsuggests this file might be mounted or created dynamically by the system or user to control probe behavior.
Mermaid Flowchart Diagram
flowchart TD
A[Start] --> B{File /data/disable_startup exists?}
B -- Yes --> C[Print "startup probe disabled"]
C --> D[Exit 0]
B -- No --> E[Send JSON-RPC POST to localhost:8545]
E --> F{curl successful?}
F -- No --> G[Exit 1]
F -- Yes --> H[Parse JSON response to get .result]
H --> I{Is .result == true?}
I -- Yes --> J[Print "daemon is listening"]
J --> D
I -- No --> K[Print "daemon is not listening"]
K --> G
Summary
`startup.sh` is a concise and effective shell script that acts as a startup readiness probe by querying a local daemon's JSON-RPC interface to confirm if it is actively listening on the network. It supports a simple disable switch and cleanly reports status, making it well-suited for integration in service orchestration and monitoring workflows.