startup.sh
Overview
`startup.sh` is a lightweight Bash script designed to perform a simple startup health check for a local service running on the host machine. Its primary function is to verify the availability of a service listening at `http://localhost:26657` by querying its `/status` endpoint. The script includes a conditional mechanism to disable the startup probe if a specific file (`/root/disable_startup`) exists, which allows for manual override or temporary bypass of the health check.
This script is typically used as a startup probe in containerized environments (e.g., Kubernetes) or init scripts to ensure that dependent services are available before proceeding with further application initialization or readiness signaling.
Script Breakdown and Explanation
#!/bin/bash
DISABLE_STARTUP_PROBE=/root/disable_startup
if [[ -f "$DISABLE_STARTUP_PROBE" ]]; then
echo "startup probe disabled"
exit 0
fi
curl -sf http://localhost:26657/status && exit 0 || exit 1
1. Shebang
#!/bin/bash
Specifies the script interpreter as Bash. Ensures compatibility and correct execution in Unix-like environments.
2. Variable Declaration
DISABLE_STARTUP_PROBE=/root/disable_startup
Defines a constant path variable that points to the file used to disable the startup probe check.
3. Disable Probe Check
if [[ -f "$DISABLE_STARTUP_PROBE" ]]; then
echo "startup probe disabled"
exit 0
fi
Checks if the file
/root/disable_startupexists.If it does, the script prints a message indicating the probe is disabled and exits successfully (
exit 0).This allows operators or automated processes to temporarily disable the startup probe without modifying the script.
4. Health Check Using curl
curl -sf http://localhost:26657/status && exit 0 || exit 1
Uses
curlto silently (-s) and fail silently on server errors (-f) request the/statusendpoint on localhost port 26657.If the request is successful (HTTP 2xx response), the script exits with a success status (
exit 0).If the request fails (connection refused, timeout, or non-2xx response), it exits with failure (
exit 1).
This simple mechanism provides a binary health indication that can be used by orchestrators or monitoring systems.
Usage Example
To use this script as a startup health check, you might:
Copy
startup.shinto your container image or host system.Set executable permissions:
chmod +x startup.shExecute the script manually or configure it to run via container startup probes or init systems:
./startup.shIf you want to disable the health check temporarily:
touch /root/disable_startup ./startup.sh # Output: startup probe disabled
Important Implementation Details
Disabling Mechanism: The use of a file as a "flag" to disable the probe is a lightweight and effective method to toggle behavior without changing code or configuration files.
Health Endpoint: The script assumes the service provides a
/statusendpoint onlocalhost:26657. This endpoint should be implemented by the service to return a simple HTTP 200 status when healthy.Exit Codes: Exit codes conform to Unix conventions:
0for success and1for failure. This behavior is critical for integration with container orchestrators that rely on exit codes to determine container readiness or liveness.
Interaction with Other System Components
Service Under Test: The script depends on a local service listening on port 26657, likely part of the same application stack or pod/container.
Container Orchestrators / Init Systems: The script is intended to be used as a startup probe or health check script. Orchestrators like Kubernetes can execute this script to decide if a container is ready to receive traffic or should be restarted.
Operators: The presence of the
/root/disable_startupfile allows system operators or deployment scripts to disable the startup probe without modifying the script or container image.
Mermaid Flowchart Diagram
flowchart TD
A[Start: Execute startup.sh] --> B{Check if /root/disable_startup exists}
B -- Yes --> C[Print "startup probe disabled"]
C --> D[Exit with 0 (success)]
B -- No --> E[Execute curl -sf http://localhost:26657/status]
E -- Success --> F[Exit with 0 (success)]
E -- Failure --> G[Exit with 1 (failure)]
Summary
This script serves as a minimal but effective readiness or startup probe for a local service. It integrates smoothly into containerized environments or startup sequences, offering a simple disable mechanism and straightforward health check logic. Its compact design and clear exit codes make it ideal for automation and system orchestration workflows.