39 lines
826 B
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
# shellcheck disable=SC1091
source "$SCRIPT_DIR/common.sh"
if has_systemd_unit; then
systemctl start "$SYSTEMD_SERVICE_NAME"
systemctl is-active "$SYSTEMD_SERVICE_NAME"
exit 0
fi
if is_running; then
echo "hy-app-monitor already running pid=$(read_pid)"
exit 0
fi
if [ ! -f "$TARGETS_FILE" ]; then
echo "targets file not found: $TARGETS_FILE" >&2
exit 1
fi
export APP_BIND APP_PORT TARGETS_FILE PYTHONUNBUFFERED=1
nohup "$PYTHON_BIN" "$ROOT_DIR/server.py" >>"$LOG_FILE" 2>&1 &
pid=$!
echo "$pid" >"$PID_FILE"
sleep 1
if kill -0 "$pid" >/dev/null 2>&1; then
echo "hy-app-monitor started pid=$pid port=$APP_PORT"
exit 0
fi
echo "hy-app-monitor failed to start" >&2
tail -n 40 "$LOG_FILE" >&2 || true
exit 1