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


Script Workflow

  1. Disable Probe Check

    if [[ -f "$DISABLE_STARTUP_PROBE" ]]; then
      echo "startup probe disabled"
      exit 0
    fi
    
    • Checks if the file /data/disable_startup exists.

    • 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.

  2. 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 1
    
    • Uses curl to send a POST request to http://localhost:8545 with a JSON payload requesting the net_listening method.

    • -s silences progress and errors.

    • -f causes curl to fail silently on HTTP errors (non-2xx responses).

    • If curl fails (e.g., daemon not reachable), the script exits with failure (1).

  3. Parse the JSON Response

    LISTENING=$(echo $NET_LISTENING | jq -r '.result')
    
    • Pipes the JSON response into jq to extract the .result field.

    • .result is expected to be a boolean (true or false).

  4. Evaluate Daemon Status

    if [[ $LISTENING == true ]]; then
      echo "daemon is listening"
      exit 0
    fi
    
    echo "daemon is not listening"
    exit 1
    
    • If .result is true, 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


Interaction with Other System Components


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


*End of documentation*