startup.sh


Overview

`startup.sh` is a lightweight Bash startup probe script designed to check whether a daemon process, presumably an Ethereum JSON-RPC node or similar service, is actively listening for network requests on the local machine. The script determines readiness by sending a JSON-RPC request to the daemon’s HTTP interface and interpreting the response.

The script functions as a health check utility, commonly used in containerized or orchestrated environments (e.g., Kubernetes) to confirm that the service is operational before routing traffic to it. It also supports a simple override mechanism to disable the startup probe by the presence of a specific file.


Functionality and Workflow

  1. Startup Probe Disable Check:
    The script first checks for the existence of a "disable probe" marker file (/data/disable_startup). If this file is present, the script outputs "startup probe disabled" and exits successfully (exit 0). This mechanism allows operators or other processes to skip the startup check if desired.

  2. Send JSON-RPC net_listening Request:
    If the probe is not disabled, the script sends a POST request with the JSON-RPC payload {"jsonrpc":"2.0","method":"net_listening","params":[],"id":1} to http://localhost:8545. This endpoint is the default port for many Ethereum-compatible JSON-RPC services.

  3. Parse Response:
    The script parses the JSON response using jq to extract the .result field, which indicates whether the daemon is listening (true or false).

  4. Exit Status Based on Listening State:

    • If .result is true, the script prints "daemon is listening" and exits with success (exit 0).

    • If .result is false or if the request fails, it prints "daemon is not listening" and exits with failure (exit 1).


Detailed Explanation of Script Components

Variables


Main Logic (Step-by-Step)

if [[ -f "$DISABLE_STARTUP_PROBE" ]]; then
  echo "startup probe disabled"
  exit 0
fi
NET_LISTENING=$(curl -sf -d '{"jsonrpc":"2.0","method":"net_listening","params":[],"id":1}' -H 'Content-Type: application/json' http://localhost:8545) || exit 1
LISTENING=$(echo $NET_LISTENING | jq -r '.result')
if [[ $LISTENING == true ]]; then
  echo "daemon is listening"
  exit 0
fi

echo "daemon is not listening"
exit 1

Usage Example

This script is typically invoked by an orchestrator or as part of a container lifecycle management:

./startup.sh

Important Implementation Details


Interaction with Other System Components


Visual Diagram: Flowchart of startup.sh Workflow

flowchart TD
    A[Start script] --> B{Does /data/disable_startup exist?}
    B -- Yes --> C[Print "startup probe disabled"]
    C --> D[Exit 0]
    B -- No --> E[Send JSON-RPC net_listening request to localhost:8545]
    E --> F{Did curl succeed?}
    F -- No --> G[Exit 1]
    F -- Yes --> H[Parse JSON response with jq]
    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


This documentation should help developers, DevOps engineers, and system administrators understand the purpose, operation, and integration points of `startup.sh` within the overall system.