startup.sh
Overview
`startup.sh` is a lightweight shell script designed to act as a startup health probe for a service running locally on the machine. Its primary function is to determine whether the service should be considered "ready" by checking for a disabling flag file and then probing the service's HTTP status endpoint. This script is typically used in containerized or orchestrated environments (e.g., Kubernetes) as a startup probe to delay traffic routing to the service until it confirms readiness.
Detailed Explanation
Script Functionality
The script first checks for the existence of a specific file (
/root/disable_startup).If the file exists, the script prints a message indicating that the startup probe is disabled and exits with a success status (
0).If the file does not exist, the script makes an HTTP GET request to the local service's
/statusendpoint on port27147.Depending on the response from the HTTP request:
If the request succeeds (HTTP status code 2xx), the script exits with a success status (
0).If the request fails (no response or non-2xx status), the script exits with a failure status (
1).
Script Breakdown
#!/bin/bash
Shebang line: Specifies that the script should be executed using the Bash shell.
DISABLE_STARTUP_PROBE=/root/disable_startup
Variable: Path to the file that, if present, disables the startup probe.
if [[ -f "$DISABLE_STARTUP_PROBE" ]]; then
echo "startup probe disabled"
exit 0
fi
Conditional check: Tests if the disabling file exists.
Behavior: If file exists, prints a message and exits successfully to skip the probe.
curl -sf http://localhost:27147/status && exit 0 || exit 1
Command: Uses
curlto silently (-s) and fail silently (-f) make an HTTP GET request tohttp://localhost:27147/status.Logic:
If the curl command succeeds (HTTP 2xx response), exit with status
0(success).If the curl command fails (no response or error), exit with status
1(failure).
Parameters and Return Values
This script does not take any command-line parameters and returns exit codes to indicate readiness:
Exit Code | Meaning |
|---|---|
0 | Startup probe passed (service ready or probe disabled) |
1 | Startup probe failed (service not ready) |
Usage Example
This script is typically invoked by the system or orchestration platform as a startup probe.
./startup.sh
If the disable file exists, the output will be:
startup probe disabled
If the service is healthy and responding on port 27147, there is no output, but the exit code is
0.If the service is not responding, the script exits with code
1.
Implementation Details and Algorithms
Disabling Mechanism: The probe can be dynamically disabled by creating the file
/root/disable_startup. This is useful for quickly bypassing readiness checks without modifying the script or container configuration.Health Check: The script relies on a simple HTTP GET request to a local status endpoint. The use of
curl -sfensures silent operation and immediate failure on HTTP errors, making it efficient as a health check.Exit Code Semantics: The script uses exit codes to communicate status to the calling environment (e.g., Kubernetes startup probe).
Interaction with Other System Components
Service on port 27147: The script expects a service to be running locally on port
27147and exposing a/statusHTTP endpoint that returns a success status when ready.Container Orchestration: This script is ideal for use as a startup probe in container orchestrators like Kubernetes, enabling intelligent startup sequencing.
Configuration Management: The presence or absence of
/root/disable_startupallows operators or automation tools to toggle probe behavior without redeploying or changing code.
Summary Diagram
The following flowchart illustrates the logic flow of the `startup.sh` script:
flowchart TD
A[Start] --> B{Is /root/disable_startup file present?}
B -- Yes --> C[Print "startup probe disabled"]
C --> D[Exit 0]
B -- No --> E[Send HTTP GET to http://localhost:27147/status]
E --> F{Request successful?}
F -- Yes --> D
F -- No --> G[Exit 1]
Conclusion
`startup.sh` is a minimal yet effective startup readiness probe script that balances simplicity and functionality. It allows easy disabling of the probe for maintenance or troubleshooting and performs a straightforward health check by querying a local service endpoint. Its design facilitates smooth integration with container orchestration systems to ensure that services only receive traffic when fully ready.