startup.sh
Overview
`startup.sh` is a lightweight shell script designed to serve as a startup probe for a service or container environment. Its primary purpose is to determine whether the service is ready to start or continue running by performing a simple health check. The script checks for a "disable startup probe" flag file; if present, it skips further checks and exits successfully. Otherwise, it attempts to verify the service status by making an HTTP request to a local endpoint. The script exits with a status code that reflects the health of the service, enabling container orchestration platforms (e.g., Kubernetes) or process supervisors to make informed decisions regarding service readiness.
Detailed Explanation
Script Logic and Flow
#!/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:27147/status && exit 0 || exit 1
Variables
DISABLE_STARTUP_PROBEType: String (file path)
Value:
/root/disable_startupDescription: This variable holds the path to a probe disable "flag" file. If this file exists on the filesystem, the startup probe is bypassed.
Script Steps
Check for Disable Flag
The script first checks if the file specified byDISABLE_STARTUP_PROBEexists (-ftest).If the file exists, it prints
"startup probe disabled"to standard output and exits with status code0(success), indicating that the startup probe is intentionally disabled.
Perform Health Check via HTTP Request
If the disable flag is not present, the script usescurlto silently (-s) fetch the/statusendpoint onhttp://localhost:27147.The
-foption tellscurlto fail silently on server errors (HTTP status codes >= 400).If the HTTP request succeeds, the script exits with status
0indicating the service is ready.If the request fails (e.g., connection refused, server error), the script exits with status
1, signaling the service is not ready.
Parameters and Return Values
This script takes no parameters and relies on the environment and local files. The exit code is crucial for external consumers:
Exit Code
0Startup probe disabled, or
Service responded successfully to the status check.
Exit Code
1Service did not respond successfully (e.g., not ready, down, or unhealthy).
Usage Example
This script is typically invoked by a container runtime or orchestration platform as a startup probe:
# Example usage in a Kubernetes Pod spec:
startupProbe:
exec:
command:
- /bin/bash
- /path/to/startup.sh
initialDelaySeconds: 5
periodSeconds: 10
failureThreshold: 30
Here, the orchestration platform runs `startup.sh` repeatedly until it exits with status `0`, indicating the service is ready.
Important Implementation Details
Disable Flag File: The presence of
/root/disable_startupallows operators or automation to bypass the startup probe without modifying deployment manifests or scripts. This is useful during debugging or in environments where the startup probe is not desired.HTTP Health Check: The script assumes that the monitored service exposes a health/status endpoint on port
27147locally. The choice of port and endpoint is specific to the service and must be running and responsive for the probe to succeed.Silent Curl: Using
curl -sfensures no output is shown in logs unless there is an error, and the command fails cleanly on server errors.Exit Codes: The script’s exit codes directly influence orchestration behavior; thus, it is critical that these codes correctly represent service readiness.
Interaction with Other System Components
Service Endpoint: The script depends on the local service exposing an HTTP status endpoint at
http://localhost:27147/status. This endpoint should return an HTTP 200 status code when the service is healthy.Container Orchestration Platform: The script is intended to be invoked by the container platform (e.g., Kubernetes) as a startup probe. The platform uses the exit code to determine whether to proceed with starting or restarting the container.
Flag File Management: External processes or operators may create or delete the
/root/disable_startupfile to enable or disable the startup probe dynamically.
Visual Diagram: Startup Probe Flow
flowchart TD
A[Start startup.sh script] --> B{Check if /root/disable_startup file exists}
B -- Yes --> C[Print "startup probe disabled"]
C --> D[Exit with status 0]
B -- No --> E[Send HTTP GET to http://localhost:27147/status]
E --> F{HTTP request successful?}
F -- Yes --> G[Exit with status 0]
F -- No --> H[Exit with status 1]
Summary
The `startup.sh` script is a minimal and efficient startup readiness probe used to determine if a service is ready to accept traffic. It provides a mechanism to skip the probe when necessary and relies on a simple HTTP health check endpoint. Its exit status is key for container orchestration platforms to manage the lifecycle of services robustly.