fueanta

Bash script to install heartbeat for healthchecks.io

Jul 18th, 2026
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.49 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. set -Eeuo pipefail
  4.  
  5. readonly ENV_FILE="/etc/pi-heartbeat.env"
  6. readonly SERVICE_FILE="/etc/systemd/system/pi-heartbeat.service"
  7. readonly TIMER_FILE="/etc/systemd/system/pi-heartbeat.timer"
  8.  
  9. usage() {
  10.     cat <<'EOF'
  11. Usage:
  12.   sudo ./install-heartbeat.sh <ping-url>
  13.  
  14. Example:
  15.   sudo ./install-heartbeat.sh https://hc-ping.com/your-check-uuid
  16. EOF
  17. }
  18.  
  19. if [[ ${EUID} -ne 0 ]]; then
  20.     echo "Error: run this installer with sudo." >&2
  21.     usage >&2
  22.     exit 1
  23. fi
  24.  
  25. if [[ $# -ne 1 ]]; then
  26.     usage >&2
  27.     exit 1
  28. fi
  29.  
  30. ping_url=$1
  31.  
  32. case "$ping_url" in
  33.     https://* | http://*) ;;
  34.     *)
  35.         echo "Error: ping URL must begin with https:// or http://." >&2
  36.         exit 1
  37.         ;;
  38. esac
  39.  
  40. if [[ "$ping_url" == *$'\n'* || "$ping_url" == *$'\r'* || "$ping_url" == *' '* ]]; then
  41.     echo "Error: ping URL contains invalid whitespace." >&2
  42.     exit 1
  43. fi
  44.  
  45. if ! command -v systemctl >/dev/null 2>&1; then
  46.     echo "Error: systemd is required." >&2
  47.     exit 1
  48. fi
  49.  
  50. if ! command -v curl >/dev/null 2>&1; then
  51.     echo "curl is missing; installing it..."
  52.     apt-get update -qq
  53.     apt-get install -y curl
  54. fi
  55.  
  56. umask 077
  57. printf 'PING_URL=%s\n' "$ping_url" >"$ENV_FILE"
  58. chown root:root "$ENV_FILE"
  59. chmod 0600 "$ENV_FILE"
  60.  
  61. cat >"$SERVICE_FILE" <<'EOF'
  62. [Unit]
  63. Description=External availability heartbeat
  64. Wants=network-online.target
  65. After=network-online.target
  66.  
  67. [Service]
  68. Type=oneshot
  69. EnvironmentFile=/etc/pi-heartbeat.env
  70. ExecStart=/usr/bin/curl --fail --silent --show-error --max-time 10 --retry 2 --retry-delay 3 ${PING_URL}
  71. TimeoutStartSec=45
  72. NoNewPrivileges=true
  73. PrivateTmp=true
  74. ProtectHome=true
  75. ProtectSystem=strict
  76. EOF
  77.  
  78. cat >"$TIMER_FILE" <<'EOF'
  79. [Unit]
  80. Description=Send external availability heartbeat every minute
  81.  
  82. [Timer]
  83. OnBootSec=2min
  84. OnUnitActiveSec=1min
  85. AccuracySec=10s
  86. RandomizedDelaySec=10s
  87. Unit=pi-heartbeat.service
  88.  
  89. [Install]
  90. WantedBy=timers.target
  91. EOF
  92.  
  93. chown root:root "$SERVICE_FILE" "$TIMER_FILE"
  94. chmod 0644 "$SERVICE_FILE" "$TIMER_FILE"
  95.  
  96. systemctl daemon-reload
  97. systemctl enable --now pi-heartbeat.timer
  98.  
  99. echo "Sending the first heartbeat..."
  100. if systemctl start pi-heartbeat.service; then
  101.     echo "Heartbeat installed and first check-in succeeded."
  102.     echo "Timer status:"
  103.     systemctl --no-pager --full status pi-heartbeat.timer || true
  104. else
  105.     echo "Heartbeat was installed, but the first check-in failed." >&2
  106.     echo "Inspect it with: journalctl -u pi-heartbeat.service -n 30 --no-pager" >&2
  107.     exit 1
  108. fi
  109.  
Advertisement
Add Comment
Please, Sign In to add comment