startup.sh
Overview
`startup.sh` is a lightweight Bash script designed to verify the startup status of a local service called `op-node`. It acts as a startup probe that can be used in containerized or orchestrated environments (e.g., Kubernetes) to determine if the `op-node` service is operational and ready to serve requests.
The script performs the following key functions:
Checks for the presence of a "disable startup probe" flag file to optionally skip the probe.
Sends a JSON-RPC request to the local
op-nodeservice to retrieve its version.Parses the response to confirm if the service is running.
Exits with appropriate codes based on the service status to signal readiness or failure.
This enables external orchestration tools to make informed decisions about the health and readiness of the `op-node` service.
Detailed Explanation
Constants and Variables
DISABLE_STARTUP_PROBE:
Path to a file (/data/disable_startup) that, if present, disables the startup probe check.VERSION:
Stores the raw JSON response from theop-nodeservice to the JSON-RPC request querying theoptimism_versionmethod.RESULT:
Extracted version string from the JSON response usingjq.
Script Logic and Steps
1. Check if startup probe is disabled
if [[ -f "$DISABLE_STARTUP_PROBE" ]]; then
echo "startup probe disabled"
exit 0
fi
Purpose:
Allows manual or programmatic disabling of the startup probe by creating the file at/data/disable_startup.Behavior:
If the file exists, the script prints"startup probe disabled"and exits with status0(success), signaling the probe is skipped but not failed.
2. Query the op-node service for version
VERSION=$(curl -sf -d '{"jsonrpc":"2.0","method":"optimism_version","params":[],"id":1}' -H 'Content-Type: application/json' http://localhost:9545) || exit 1
Purpose:
Sends a JSON-RPC POST request to the localop-nodeservice endpointhttp://localhost:9545with the methodoptimism_versionand no parameters.Details:
curloptions:-s: silent mode, suppresses progress meter.-f: fail silently on HTTP errors (non-2xx).
If the request fails (e.g., connection refused), the script immediately exits with status
1.
3. Parse the JSON response to extract the version result
RESULT=$(echo $VERSION | jq -r '.result')
Purpose:
Usesjqto parse the JSON and extract the.resultfield, which should contain the version string if the service responded correctly.
4. Check if the version result is non-empty and determine service status
if [[ result != "" ]]; then
echo "op-node started"
exit 0
fi
echo "op-node not started"
exit 1
Purpose:
If
RESULTis non-empty, it means the service responded correctly, so the script prints"op-node started"and exits0to indicate success.Otherwise, it prints
"op-node not started"and exits1to indicate failure.
Important Note / Bug:
There is a bug here: the condition usesresult(lowercase) instead of$RESULT(uppercase variable). It should be:if [[ "$RESULT" != "" ]]; thenWithout the
$sign and correct casing, this condition will always evaluate as true becauseresultis treated as a literal string, which is not empty.
Usage Example
Run the script directly in the environment where the `op-node` service is running locally on port `9545`:
./startup.sh
If the
op-nodeservice is running and responding correctly, output will be:
op-node started
If the service is down or the probe is disabled, output will be:
startup probe disabled
or
op-node not started
Exit codes:
0= success (service started or probe disabled)1= failure (service not started or request failed)
Implementation Details and Algorithms
Health Check via JSON-RPC:
The script uses JSON-RPC protocol to query theop-nodeservice, which is a standard remote procedure call protocol encoded in JSON. This allows the script to verify the service by calling a lightweight method (optimism_version) that returns the node's version string, indicating it is operational.Use of Probe Disable Flag:
The presence of a file (/data/disable_startup) acts as a feature toggle to bypass the probe entirely, which can be useful during maintenance or debugging.Error Handling:
The script usescurl -fto fail on HTTP errors and immediate exit to prevent false positives.JSON Parsing:
The script leveragesjq, a command-line JSON processor, to extract the relevant field from the JSON response.
Interaction with Other System Components
op-nodeService:
This script depends on the localop-nodenode running an HTTP JSON-RPC server on port 9545. Theoptimism_versionmethod must be supported by this service.Orchestration/Container Management (e.g., Kubernetes):
The script is likely used as a startup readiness probe in container orchestrators. Its exit code results determine whether the container is considered ready to serve traffic.File System:
The presence or absence of the/data/disable_startupfile controls probe behavior, indicating an external process or administrator can influence readiness.
Recommended Fix
To correct the bug in the script, change:
if [[ result != "" ]]; then
to
if [[ "$RESULT" != "" ]]; then
This ensures the check evaluates the actual variable content.
Flowchart Diagram
flowchart TD
Start([Start]) --> CheckDisable{Is /data/disable_startup present?}
CheckDisable -- Yes --> PrintDisabled["Print 'startup probe disabled'"] --> ExitSuccess[Exit 0]
CheckDisable -- No --> SendRequest["Send JSON-RPC request to localhost:9545"]
SendRequest -- Fail --> ExitFail[Exit 1]
SendRequest -- Success --> ParseResponse["Parse JSON response with jq"]
ParseResponse --> CheckResult{Is result non-empty?}
CheckResult -- Yes --> PrintStarted["Print 'op-node started'"] --> ExitSuccess
CheckResult -- No --> PrintNotStarted["Print 'op-node not started'"] --> ExitFail
Summary
`startup.sh` is a concise startup readiness probe script for the `op-node` service. It conditionally disables itself, performs a JSON-RPC version query to verify service readiness, and exits with appropriate codes for use in automated orchestration. The script contains a minor bug in variable usage, which should be corrected for reliable operation. Its simple but effective design integrates tightly with container health checking mechanisms, ensuring robust startup validation.