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
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 status0(success), effectively skipping any further checks.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 endpointhttp://localhost:9545usingcurl. The request calls the method"optimism_version", which presumably returns the running version or status of the op-node.Response Parsing and Validation
The response is parsed usingjqto extract the.resultfield. 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
Shebang line specifying the script interpreter as Bash.
DISABLE_STARTUP_PROBE=/data/disable_startup
Defines path to the "disable startup probe" flag file.
if [[ -f "$DISABLE_STARTUP_PROBE" ]]; then
echo "startup probe disabled"
exit 0
fi
Checks if the
disable_startupfile exists. If yes, disables startup probe by exiting with status 0.
VERSION=$(curl -sf -d '{"jsonrpc":"2.0","method":"optimism_version","params":[],"id":1}' -H 'Content-Type: application/json' http://localhost:9545) || exit 1
Sends a silent (
-s) and fail-fast (-f) HTTP POST request with JSON-RPC payload to the node.Stores response JSON in
VERSION.If the curl command fails, exits with status 1.
RESULT=$(echo $VERSION | jq -r '.result')
Uses
jqto extract theresultfield from the JSON response.
if [[ result != "" ]]; then
echo "op-node started"
exit 0
fi
Note: This line contains a bug in variable reference—the variable
resultshould be$RESULT.If the
resultis non-empty, prints success message and exits 0.
echo "op-node not started"
exit 1
If the above condition fails, prints error message and exits 1.
Parameters
This script does not accept any command-line parameters or arguments. It relies on:
Presence or absence of the
/data/disable_startupfile.The local HTTP JSON-RPC endpoint at
http://localhost:9545.
Return Values / Exit Codes
0— Success, op-node is running or startup probe is disabled.1— Failure, op-node is not responding as expected or curl request failed.
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:
If probe disabled:
startup probe disabled
If node is running and responding:
op-node started
If node is not responsive:
op-node not started
Important Implementation Details
Disable Flag File: Presence of
/data/disable_startupimmediately disables the probe, useful for maintenance or controlled startup scenarios.JSON-RPC Request: Uses a simple JSON-RPC call
"optimism_version"to verify node health.Error Handling: Uses
curloptions-sand-ffor silent failure and immediate error detection.Response Parsing: Utilizes
jqfor robust JSON parsing.Variable Bug: The script contains a bug where it checks
if [[ result != "" ]]instead ofif [[ $RESULT != "" ]]. This will cause the condition to always evaluate to true since the unquotedresultis treated as a string literal. This should be corrected for proper operation.
Corrected snippet:
if [[ $RESULT != "" ]]; then
echo "op-node started"
exit 0
fi
Interaction with Other System Components
Op-node Process: This script communicates with the op-node running locally on port 9545 via HTTP JSON-RPC.
Container Orchestration / Monitoring: Typically executed as a Kubernetes startup, readiness, or liveness probe, or as part of deployment health checks.
File System: Checks for
/data/disable_startupwhich could be mounted or created by external processes to control probe behavior.
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.