Files
gold_monitor/deploy/lib/common.sh
T

34 lines
737 B
Bash

#!/usr/bin/env bash
# shellcheck shell=bash
detect_server_ip() {
local ip
ip="$(hostname -I 2>/dev/null | awk '{print $1}')"
if [[ -z "${ip}" ]]; then
ip="$(ip -4 route get 1.1.1.1 2>/dev/null | awk '{for(i=1;i<=NF;i++) if($i=="src"){print $(i+1); exit}}')"
fi
echo "${ip:-127.0.0.1}"
}
need_root() {
if [[ "$(id -u)" -ne 0 ]]; then
echo "错误: 请使用 root 执行" >&2
exit 1
fi
}
ensure_node_pm2() {
if ! command -v node >/dev/null 2>&1; then
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt-get install -y nodejs
fi
if ! command -v pm2 >/dev/null 2>&1; then
npm install -g pm2
fi
}
verify_http() {
local url="$1"
curl -fsS --max-time 5 "${url}" >/dev/null 2>&1
}