startup.sh
Overview
`startup.sh` is a startup probe script written in Bash designed to verify whether a critical daemon (likely an Ethereum JSON-RPC node) is active and responsive on the local machine. It performs a health check by sending a JSON-RPC request to the daemon's HTTP endpoint to determine if the daemon is currently listening for network connections.
The script first checks for the presence of a "disable startup probe" file, which, if present, causes the script to exit successfully immediately. If the probe is enabled, it sends a JSON-RPC request to the local daemon on port 8545 to check if the daemon is listening (`net_listening` method). Based on the response, it outputs the daemon's status and exits with an appropriate status code.
This script is typically used as a Kubernetes startup probe or as part of a container orchestration or system initialization process to monitor the readiness of a service before allowing dependent services to proceed.
Detailed Explanation
Variables
DISABLE_STARTUP_PROBEType: String (file path)
Description: Path to a file that, if exists, disables the startup probe. The script will exit immediately with success if this file is found.
Value:
/data/disable_startup
NET_LISTENINGType: String (JSON response)
Description: Stores the JSON response from the daemon's JSON-RPC API call to
net_listening.Value: Result of the
curlcommand querying the local daemon.
LISTENINGType: String (
trueorfalse)Description: Extracted value from the JSON response indicating whether the daemon is currently listening on the network.
Script Workflow
Disable Probe Check
if [[ -f "$DISABLE_STARTUP_PROBE" ]]; then echo "startup probe disabled" exit 0 fiChecks if the file
/data/disable_startupexists.If it exists, the script prints
"startup probe disabled"and exits with a success status (0).This mechanism allows manual or automated disabling of the startup probe without modifying the script.
Send JSON-RPC Request
NET_LISTENING=$(curl -sf -d '{"jsonrpc":"2.0","method":"net_listening","params":[],"id":1}' -H 'Content-Type: application/json' http://localhost:8545) || exit 1Uses
curlto send a POST request tohttp://localhost:8545with a JSON payload requesting thenet_listeningmethod.-ssilences progress and errors.-fcausescurlto fail silently on HTTP errors (non-2xx responses).If
curlfails (e.g., daemon not reachable), the script exits with failure (1).
Parse the JSON Response
LISTENING=$(echo $NET_LISTENING | jq -r '.result')Pipes the JSON response into
jqto extract the.resultfield..resultis expected to be a boolean (trueorfalse).
Evaluate Daemon Status
if [[ $LISTENING == true ]]; then echo "daemon is listening" exit 0 fi echo "daemon is not listening" exit 1If
.resultistrue, the script prints"daemon is listening"and exits successfully.Otherwise, it prints
"daemon is not listening"and exits with failure.
Usage Example
This script is typically executed automatically by a container orchestrator as a startup probe but can also be run manually:
$ ./startup.sh
daemon is listening
$ echo $?
0
Or with the probe disabled:
$ touch /data/disable_startup
$ ./startup.sh
startup probe disabled
$ echo $?
0
Or when the daemon is not running:
$ ./startup.sh
daemon is not listening
$ echo $?
1
Implementation Details and Algorithms
The script uses a simple JSON-RPC health check by calling the
net_listeningmethod, which is a standard Ethereum JSON-RPC method that returns a boolean indicating if the node is listening on the network.The script handles errors gracefully by:
Exiting early if the probe is disabled.
Exiting with failure if the daemon is unreachable or the
curlcommand fails.Properly parsing JSON using
jqto avoid brittle text parsing.
The exit codes (
0for success,1for failure) conform to conventional UNIX practice, allowing integration into larger monitoring or orchestration systems.
Interaction with Other System Components
Daemon (Ethereum JSON-RPC Node): The script communicates over HTTP to the daemon on localhost port 8545. It expects the daemon to implement the JSON-RPC interface and respond to
net_listening.Container Orchestration / Startup Probe: This script is well-suited to be used as a Kubernetes startup probe, where it informs the orchestrator whether the containerized daemon is ready to accept requests.
Probe Disabling Mechanism: The presence of
/data/disable_startupallows system administrators or automated processes to bypass the startup check, potentially useful during maintenance or troubleshooting.Dependencies: Requires
curlfor HTTP requests andjqfor JSON parsing. These tools must be installed in the environment where the script runs.
Visual Diagram
The following flowchart illustrates the logical flow and decision points within the `startup.sh` script:
flowchart TD
Start([Start])
CheckDisable{Is "/data/disable_startup" file present?}
ExitDisabled["Print 'startup probe disabled'\nExit 0"]
SendRequest["Send JSON-RPC 'net_listening' request\nvia curl to localhost:8545"]
CurlFail{Did curl succeed?}
ParseJson["Parse JSON response\nExtract '.result' field"]
CheckListening{Is '.result' == true?}
ExitSuccess["Print 'daemon is listening'\nExit 0"]
ExitFailure["Print 'daemon is not listening'\nExit 1"]
ExitCurlFail["Exit 1"]
Start --> CheckDisable
CheckDisable -- Yes --> ExitDisabled
CheckDisable -- No --> SendRequest
SendRequest --> CurlFail
CurlFail -- No --> ExitCurlFail
CurlFail -- Yes --> ParseJson
ParseJson --> CheckListening
CheckListening -- Yes --> ExitSuccess
CheckListening -- No --> ExitFailure
Summary
startup.shis a simple but effective startup probe script for verifying if an Ethereum JSON-RPC daemon is ready.It uses a JSON-RPC call to
net_listeningto determine daemon readiness.It supports disabling the probe by creating a specific file.
The script is designed for integration in containerized environments and automated orchestration workflows.
It gracefully handles errors and provides clear output and exit codes for automation.
*End of documentation*