startup.sh


Overview

`startup.sh` is a lightweight shell script designed to serve as a **startup probe** for checking the operational status of an "op-node" service, likely part of an Optimism blockchain node infrastructure. Its primary function is to confirm if the node is running and responsive by querying a local JSON-RPC endpoint. If a specific "disable startup" flag file exists, the script bypasses the probe check entirely.

This script is intended to be used in containerized environments or automated deployment setups (such as Kubernetes liveness/readiness probes) to inform orchestration tools whether the op-node service is ready to accept traffic.


Detailed Explanation

Script Behavior and Workflow

  1. Startup Probe Disable Check
    The script first checks for the presence of a file at /data/disable_startup. If this file exists, the script logs "startup probe disabled" and exits with status 0 (success), effectively skipping any further checks.

  2. Version Query via JSON-RPC
    If the disable file is absent, the script sends a POST request with a JSON-RPC payload to the local node endpoint http://localhost:9545 using curl. The request calls the method "optimism_version", which presumably returns the running version or status of the op-node.

  3. Response Parsing and Validation
    The response is parsed using jq to extract the .result field. If this field is non-empty, the script logs "op-node started" and exits successfully (0). Otherwise, it logs "op-node not started" and exits with failure (1).


Step-by-step Code Breakdown

#!/bin/bash
DISABLE_STARTUP_PROBE=/data/disable_startup
if [[ -f "$DISABLE_STARTUP_PROBE" ]]; then
  echo "startup probe disabled"
  exit 0
fi
VERSION=$(curl -sf -d '{"jsonrpc":"2.0","method":"optimism_version","params":[],"id":1}' -H 'Content-Type: application/json' http://localhost:9545) || exit 1
RESULT=$(echo $VERSION | jq -r '.result')
if [[ result != "" ]]; then
  echo "op-node started"
  exit 0
fi
echo "op-node not started"
exit 1

Parameters

This script does not accept any command-line parameters or arguments. It relies on:


Return Values / Exit Codes


Usage Example

Run the script directly in a shell environment where the op-node is expected to be listening on port 9545:

./startup.sh

Expected outputs:

startup probe disabled
op-node started
op-node not started

Important Implementation Details

Corrected snippet:

if [[ $RESULT != "" ]]; then
  echo "op-node started"
  exit 0
fi

Interaction with Other System Components


Visual Diagram

Below is a flowchart illustrating the decision flow and key operations within `startup.sh`:

flowchart TD
    Start([Start]) --> CheckDisableFile{Does /data/disable_startup exist?}
    CheckDisableFile -- Yes --> LogDisabled["Print 'startup probe disabled'"]
    LogDisabled --> ExitSuccess0[Exit 0]

    CheckDisableFile -- No --> SendCurlRequest["Send JSON-RPC POST request to\nhttp://localhost:9545"]
    SendCurlRequest --> CurlSuccess{Did curl succeed?}
    CurlSuccess -- No --> ExitFail1[Exit 1]
    CurlSuccess -- Yes --> ParseResult["Parse '.result' from JSON response"]

    ParseResult --> CheckResult{Is result non-empty?}
    CheckResult -- Yes --> LogStarted["Print 'op-node started'"]
    LogStarted --> ExitSuccess0

    CheckResult -- No --> LogNotStarted["Print 'op-node not started'"]
    LogNotStarted --> ExitFail1

Summary

`startup.sh` is a concise, pragmatic startup probe script tailored for verifying the readiness of an op-node service through a JSON-RPC call. It supports disabling via a flag file and cleanly communicates the node status for integration with monitoring or orchestration systems. Correcting the minor variable reference bug will ensure reliable operation.


If integrated into a larger system, this script provides a simple yet effective mechanism for health-checking a critical blockchain node component in automated deployment environments.