startup.sh


Overview

`startup.sh` is a Bash shell script designed as a startup probe for a daemon process that exposes a JSON-RPC interface, specifically targeting an Ethereum-like node running on `localhost:8545`. The script checks whether the daemon is actively listening for network connections and whether it has any active peers connected. This is typically used in containerized environments (e.g., Kubernetes) as a readiness or liveness probe to determine if the daemon is ready to serve requests.

Key features:


Detailed Explanation

Key Variables


Script Workflow and Logic

  1. Disable Check

    The script first checks if the file /data/disable_startup exists. If present, it outputs:

    startup probe disabled
    

    and exits with status 0, bypassing all further checks.

  2. RPC Calls

    The script sends two HTTP POST requests to the daemon's JSON-RPC endpoint at http://localhost:8545:

    • net_listening: Checks whether the node is listening for network connections.

    • net_peerCount: Retrieves the number of connected peers, returned as a hex string.

    Both requests use `curl` with silent fail mode (`-sf`), and if either fails (e.g., no response), the script exits with status `1`.

  3. Parsing Responses

    The responses are JSON objects. The script uses jq to extract the .result field from each:

    • LISTENING holds "true" or "false" as a string.

    • PEER_COUNT_HEX is a hexadecimal string representing the peer count (e.g., "0x3").

    The peer count hex string is converted to a decimal integer (`PEER_COUNT`).

  4. Decision Logic

    • If LISTENING is true:

      • If PEER_COUNT > 0:

        daemon is listening, with <PEER_COUNT> peers
        

        Exit 0.

      • Else:

        daemon is listening, but has no peers
        

        Exit 1.

    • If LISTENING is not true:

      daemon is not listening
      

      Exit 1.


Usage Example

This script is intended to be run as part of a container startup probe or manually to check daemon readiness.

# Run the probe manually
./startup.sh

# Possible outputs:
# "startup probe disabled" (exit 0)
# "daemon is listening, with 5 peers" (exit 0)
# "daemon is listening, but has no peers" (exit 1)
# "daemon is not listening" (exit 1)

Implementation Details


Interaction with Other System Components


Visual Diagram: Flowchart of Script Workflow

flowchart TD
    Start([Start])
    CheckDisable{Is /data/disable_startup present?}
    ExitDisabled["Echo 'startup probe disabled'\nExit 0"]
    RPCNetListening["POST net_listening RPC\nGet LISTENING"]
    RPCNetPeerCount["POST net_peerCount RPC\nGet PEER_COUNT_HEX"]
    ConvertHex["Convert PEER_COUNT_HEX to PEER_COUNT"]
    CheckListening{Is LISTENING == true?}
    CheckPeers{Is PEER_COUNT > 0?}
    ListeningWithPeers["Echo 'daemon is listening, with PEER_COUNT peers'\nExit 0"]
    ListeningNoPeers["Echo 'daemon is listening, but has no peers'\nExit 1"]
    NotListening["Echo 'daemon is not listening'\nExit 1"]
    CurlFail["Curl failed\nExit 1"]

    Start --> CheckDisable
    CheckDisable -- Yes --> ExitDisabled
    CheckDisable -- No --> RPCNetListening
    RPCNetListening -- Success --> RPCNetPeerCount
    RPCNetListening -- Fail --> CurlFail
    RPCNetPeerCount -- Success --> ConvertHex
    RPCNetPeerCount -- Fail --> CurlFail
    ConvertHex --> CheckListening
    CheckListening -- Yes --> CheckPeers
    CheckListening -- No --> NotListening
    CheckPeers -- Yes --> ListeningWithPeers
    CheckPeers -- No --> ListeningNoPeers

Summary

`startup.sh` is a concise and effective startup probe script that verifies the readiness of a JSON-RPC enabled daemon by checking network listening status and connected peer count. It provides clear exit codes and messages for integration with container orchestration systems or deployment automation. The script's simplicity and reliance on standard tools (`curl`, `jq`, Bash) make it portable and easy to maintain.