#!/usr/bin/env bash # One entry point for the bare-metal systemd service: install, start, stop, status, logs, # update, uninstall. Run from anywhere; the script uses the repo it lives in. # # scripts/install-as-service.sh # # install write /etc/systemd/system/vllm.service for THIS repo directory, # write /etc/default/vllm (checkpoint path, model name) when missing, # install the logrotate policy, daemon-reload, enable # start|stop|restart|status|logs systemctl for the service # update ./build.sh in this repo, then restart the service # uninstall disable + remove the unit, the EnvironmentFile and the logrotate file # # Needs root (the log goes to /var/log, the unit goes to /etc/systemd/system). # The service runs the repo where it lives: no copy, no /opt, no second build tree. # Env: CKPT MODEL_NAME PORT override what the EnvironmentFile sets. set -euo pipefail HERE="$(cd "$(dirname "$0")/.." && pwd)" UNIT=vllm UNIT_PATH=/etc/systemd/system/$UNIT.service DEFAULT=/etc/default/$UNIT LOGROTATE=/etc/logrotate.d/$UNIT LOG="${LOG:-/var/log/vllm.log}" [ "$(id -u)" = 0 ] || { echo "run as root: sudo $0 $*"; exit 1; } command -v systemctl >/dev/null || { echo "no systemd on this host"; exit 1; } cmd="${1:-}"; shift || true case "$cmd" in install) ckpt="${CKPT:-$(sed -n 's/^MODEL_DIR=//p' "$HERE/.env" 2>/dev/null | head -1)}" ckpt="${ckpt:-/data/models/halt95/Swift1.5-Qwen3.8-Flash-Next-W4A16-Merlin}" [ -f "$HERE/start.sh" ] || { echo "not the repo root: $HERE"; exit 1; } # 1. the unit: the shipped template, with /opt/vllm replaced by this directory sed "s|/opt/vllm|$HERE|g" "$HERE/systemd/$UNIT.service" > "$UNIT_PATH" echo "==> unit: $UNIT_PATH (WorkingDirectory=$HERE)" # 2. the EnvironmentFile: create once, never overwrite an edited one if [ ! -f "$DEFAULT" ]; then { echo "# Read by $UNIT.service. Uncomment and edit, then: systemctl restart $UNIT" echo "CKPT=$ckpt" echo "MODEL_NAME=Scarlet" echo "# LOG=/var/log/vllm.log" echo "# CACHE_ROOT=$HERE/.vllm-cache" echo "# CUDA_VISIBLE_DEVICES=0,1,2,3" echo "# VLLM_LOG_STATS_INTERVAL=1" } > "$DEFAULT" echo "==> wrote $DEFAULT: set CKPT to the checkpoint directory" else echo "==> kept $DEFAULT (it exists; CKPT=$(. "$DEFAULT" 2>/dev/null && echo "${CKPT:-unset}" || echo unset))" fi # 3. log rotation for $LOG (same policy as the shipped systemd/vllm.logrotate) sed "s|/var/log/vllm.log|$LOG|g" "$HERE/systemd/$UNIT.logrotate" > "$LOGROTATE" chmod 644 "$LOGROTATE" echo "==> logrotate: $LOGROTATE" systemctl daemon-reload systemctl enable "$UNIT" ck="$( . "$DEFAULT" 2>/dev/null && echo "${CKPT:-}" )" [ -n "$ck" ] && [ -d "$ck" ] || echo "==> next: edit CKPT in $DEFAULT (the checkpoint directory), then sudo $0 start" echo "==> installed and enabled. Start with: sudo $0 start" ;; start) systemctl start "$UNIT"; echo "==> started; a cold start takes ~6 min: sudo $0 logs" ;; stop) systemctl stop "$UNIT" ;; restart) systemctl restart "$UNIT" ;; status) systemctl status "$UNIT" --no-pager -l | head -12 || : echo "==> engine log: tail -f $LOG (healthy line: GPU KV cache size)" ;; logs) exec tail -n 100 -f "$LOG" ;; update) echo "==> build, then restart" ( cd "$HERE" && ./build.sh ) systemctl restart "$UNIT" echo "==> update done" ;; uninstall) printf 'remove the unit, the EnvironmentFile and the logrotate file? [y/N] ' read -r a case "$a" in [yY]*) systemctl disable --now "$UNIT" 2>/dev/null || : rm -f "$UNIT_PATH" "$DEFAULT" "$LOGROTATE" systemctl daemon-reload echo "==> removed (the repo, .venv, .vllm-cache and $LOG are kept)" ;; *) echo "cancelled" ;; esac ;; *) sed -n '3,18p' "$0" exit 1 ;; esac