From 9b7f2ad745a63c974632bfb5861d58044aaf86dd Mon Sep 17 00:00:00 2001 From: kanshan Date: Mon, 3 Mar 2025 13:26:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 8ollama.sh | 189 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 8ollama.sh diff --git a/8ollama.sh b/8ollama.sh new file mode 100644 index 0000000..be98b2f --- /dev/null +++ b/8ollama.sh @@ -0,0 +1,189 @@ +#!/bin/bash + +# 定义基础变量 +SERVICE_NAME="ollama" +SERVICE_DIR="/etc/systemd/system" +OLLAMA_BIN="/usr/local/bin/ollama" +BASE_PORT=11434 +USER="ollama" +GROUP="ollama" + +# 检查是否以 root 用户运行 +if [ "$EUID" -ne 0 ]; then + echo "请以 root 用户运行此脚本。" + exit 1 +fi + +# 检查 OLLAMA_BIN 是否存在 +if [ ! -f "$OLLAMA_BIN" ]; then + echo "错误: $OLLAMA_BIN 不存在,请确认 Ollama 已安装。" + exit 1 +fi + +# 获取系统中的 GPU 数量 +get_gpu_count() { + GPU_COUNT=$(nvidia-smi --list-gpus | wc -l) + if [ "$GPU_COUNT" -eq 0 ]; then + echo "未检测到 GPU,请确保 NVIDIA 驱动已安装。" + exit 1 + fi + echo "检测到 $GPU_COUNT 个 GPU。" +} + +# 创建服务文件 +create_services() { + get_gpu_count + for ((i=0; i /dev/null +[Unit] +Description=Ollama Service (GPU $i) +After=network-online.target + +[Service] +ExecStart=$OLLAMA_BIN serve +User=$USER +Group=$GROUP +Restart=always +RestartSec=3 +Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin" +Environment="OLLAMA_NUM_PARALLEL=$GPU_COUNT" +Environment="OLLAMA_HOST=0.0.0.0:$PORT" +Environment="CUDA_VISIBLE_DEVICES=$i" +Environment="OLLAMA_ORIGINS=*" + +[Install] +WantedBy=default.target +EOF + + echo "服务文件 $SERVICE_FILE 创建成功。" + done + + # 重新加载 systemd 配置 + echo "重新加载 systemd 配置..." + sudo systemctl daemon-reload +} + +# 启动所有服务 +start_services() { + get_gpu_count + for ((i=0; i