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


Script Steps

  1. Check for Disable Flag
    The script first checks if the file specified by DISABLE_STARTUP_PROBE exists (-f test).

    • If the file exists, it prints "startup probe disabled" to standard output and exits with status code 0 (success), indicating that the startup probe is intentionally disabled.

  2. Perform Health Check via HTTP Request
    If the disable flag is not present, the script uses curl to silently (-s) fetch the /status endpoint on http://localhost:27147.

    • The -f option tells curl to fail silently on server errors (HTTP status codes >= 400).

    • If the HTTP request succeeds, the script exits with status 0 indicating 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:


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


Interaction with Other System Components


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.