chenyc
2026-04-21 8632fbd73fdb15f22fae9cd36b9ed3e0635360f1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env bash
set -euo pipefail
 
SCRIPT_DIR="$(cd -- "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(cd -- "${SCRIPT_DIR}/.." && pwd)"
SERVICE_NAME="${SERVICE_NAME:-jhm-service}"
SERVICE_USER="${SERVICE_USER:-root}"
INSTALL_DIR="${INSTALL_DIR:-$ROOT_DIR}"
TEMPLATE_PATH="${SCRIPT_DIR}/jhm-service.service.tpl"
TARGET_PATH="/etc/systemd/system/${SERVICE_NAME}.service"
TEMP_PATH="$(mktemp)"
 
if [[ ! -f "${ROOT_DIR}/jhm-service" ]]; then
  echo "Executable not found: ${ROOT_DIR}/jhm-service" >&2
  exit 1
fi
 
if [[ ! -f "${ROOT_DIR}/runtime/config.json" ]]; then
  echo "Config file not found: ${ROOT_DIR}/runtime/config.json" >&2
  exit 1
fi
 
chmod +x "${ROOT_DIR}/jhm-service"
 
sed \
  -e "s|__INSTALL_DIR__|${INSTALL_DIR}|g" \
  -e "s|__SERVICE_NAME__|${SERVICE_NAME}|g" \
  -e "s|__SERVICE_USER__|${SERVICE_USER}|g" \
  "${TEMPLATE_PATH}" > "${TEMP_PATH}"
 
sudo install -m 0644 "${TEMP_PATH}" "${TARGET_PATH}"
rm -f "${TEMP_PATH}"
 
sudo systemctl daemon-reload
sudo systemctl enable "${SERVICE_NAME}"
sudo systemctl restart "${SERVICE_NAME}"
sudo systemctl status "${SERVICE_NAME}" --no-pager