Service Script Library
This document provides a collection of production-ready service scripts.
Example 1: API Health Check
File: api-health.bash
bash
1#!/bin/bash
2
3# --- Manifest --- #
4# name: API Health
5# description: Checks the health of an API that has a health check endpoint
6# version: 1.0
7# schedule: *:0/01:00
8# timeout: 10
9
10# --- Overview --- #
11# Calls the health check endpoint and verifies the response.
12# Returns "OK" if response matches expected JSON.
13# Returns "WARNING" if response differs.
14# Returns "FAILURE" if API is unreachable.
15
16# --- Standard Constants --- #
17STATUS_OK="OK"
18STATUS_UPDATE="UPDATE"
19STATUS_WARNING="WARNING"
20STATUS_FAILURE="FAILURE"
21TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S")
22
23# --- Configurations --- #
24URL="https://api.example.com/health/"
25read -r -d '' EXPECTED_RESPONSE << 'EOF'
26{"status": "healthy"}
27EOF
28
29# --- Main --- #
30HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" "$URL" 2>/dev/null)
31
32if [ "$HTTP_CODE" != "200" ]; then
33 echo "$TIMESTAMP, $STATUS_FAILURE, API unreachable (code: $HTTP_CODE)"
34 exit 1
35fi
36
37RESPONSE=$(curl -s "$URL" 2>/dev/null)
38
39if [[ "$EXPECTED_RESPONSE" == "$RESPONSE" ]]; then
40 echo "$TIMESTAMP, $STATUS_OK, API is healthy"
41else
42 echo "$TIMESTAMP, $STATUS_WARNING, Response mismatch"
43fiExample 2: System Updates
File: system-updates.bash
bash
1#!/bin/bash
2
3# --- Manifest --- #
4# name: System Updates
5# description: Checking for system updates on Debian/Ubuntu
6# version: 1.0
7# schedule: *:0/01:00
8# timeout: 20
9
10# --- Overview --- #
11# Checks for available updates on a Debian system.
12# Returns "OK" if system is up to date.
13# Returns "UPDATE" if updates are available.
14
15# --- Standard Constants --- #
16STATUS_OK="OK"
17STATUS_UPDATE="UPDATE"
18STATUS_WARNING="WARNING"
19STATUS_FAILURE="FAILURE"
20TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S")
21
22# --- Configurations --- #
23MSG_OK="System is up to date"
24
25# --- Dependencies --- #
26# apt-check (from update-notifier-common package)
27
28# --- Main --- #
29updates=$(/usr/lib/update-notifier/apt-check 2>&1)
30IFS=';' read -ra update_array <<< "$updates"
31total_updates=${update_array[0]}
32security_updates=${update_array[1]}
33
34if [ "$total_updates" -gt 0 ]; then
35 echo "$TIMESTAMP, $STATUS_UPDATE, $total_updates updates ($security_updates security)"
36else
37 echo "$TIMESTAMP, $STATUS_OK, $MSG_OK"
38fiExample 3: Disk Usage Monitor
File: disk-usage.bash
bash
1#!/bin/bash
2
3# --- Manifest --- #
4# name: Disk Usage
5# description: Monitors root partition disk usage
6# version: 1.0
7# schedule: *:0/15:00
8# timeout: 10
9
10# --- Standard Constants --- #
11STATUS_OK="OK"
12STATUS_WARNING="WARNING"
13STATUS_FAILURE="FAILURE"
14TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S")
15
16# --- Configurations --- #
17MOUNT_POINT="/"
18WARNING_THRESHOLD=80
19CRITICAL_THRESHOLD=95
20
21# --- Main --- #
22USAGE=$(df -h "$MOUNT_POINT" | awk 'NR==2 {print $5}' | tr -d '%')
23
24if [ "$USAGE" -ge "$CRITICAL_THRESHOLD" ]; then
25 echo "$TIMESTAMP, $STATUS_FAILURE, Critical: ${USAGE}%"
26 exit 1
27elif [ "$USAGE" -ge "$WARNING_THRESHOLD" ]; then
28 echo "$TIMESTAMP, $STATUS_WARNING, High usage: ${USAGE}%"
29else
30 echo "$TIMESTAMP, $STATUS_OK, Normal: ${USAGE}%"
31fiExample 4: Long-Running ZFS Scrub
File: zpool-health.bash
bash
1#!/bin/bash
2
3# --- Manifest --- #
4# name: ZFS Zpool Health
5# description: Checks the health of ZFS zpools
6# version: 1.0
7# schedule: *-*-* 06:00:00
8# timeout: 300000
9
10# --- Standard Constants --- #
11STATUS_OK="OK"
12STATUS_UPDATE="UPDATE"
13STATUS_WARNING="WARNING"
14STATUS_FAILURE="FAILURE"
15TIMESTAMP=$(date +"%Y-%m-%d %H:%M:%S")
16
17# --- Configurations --- #
18POOL_NAME="data_pool"
19
20# --- Dependencies --- #
21# zpool (ZFS utilities)
22
23# --- Main --- #
24if ! command -v zpool &> /dev/null; then
25 echo "$TIMESTAMP, $STATUS_FAILURE, zpool command not found"
26 exit 1
27fi
28
29zpool scrub "$POOL_NAME" &> /dev/null
30
31# Report progress
32while zpool status "$POOL_NAME" | grep -q 'scrub in progress'; do
33 echo "$(date +"%Y-%m-%d %H:%M:%S"), $STATUS_UPDATE, Scrub in progress..."
34 sleep 30
35done
36
37# Check final status
38POOL_STATUS=$(zpool status "$POOL_NAME" | grep "state:" | awk '{print $2}')
39
40if [ "$POOL_STATUS" = "ONLINE" ]; then
41 echo "$TIMESTAMP, $STATUS_OK, Pool is healthy"
42else
43 echo "$TIMESTAMP, $STATUS_FAILURE, Pool state: $POOL_STATUS"
44 exit 1
45fiKey Takeaways
- Always include all required blocks (Shebang, Manifest, Standard Constants, Main)
- Use configuration variables for customizable values
- Document dependencies for transparency
- Use appropriate status codes for each scenario
- Exit with proper codes (0 for success, 1 for failure)