Files
gangyan/langchain-chat/restart.sh

100 lines
2.9 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 确保使用 bash 运行
if [ -z "$BASH_VERSION" ]; then
exec bash "$0" "$@"
fi
# 颜色定义
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# 打印带颜色的消息
print_yellow() { printf "${YELLOW}%s${NC}\n" "$1"; }
print_green() { printf "${GREEN}%s${NC}\n" "$1"; }
print_red() { printf "${RED}%s${NC}\n" "$1"; }
print_yellow "=== 停止 7861 和 8501 端口服务 ==="
for port in 7861 8501; do
pids=$(lsof -t -i:"$port" 2>/dev/null)
if [ -n "$pids" ]; then
print_yellow "正在停止端口 $port 的进程: $pids"
kill -9 $pids 2>/dev/null
print_green "端口 $port 已停止"
else
echo "端口 $port 无运行中的服务"
fi
done
# 也停止所有 startup.py 进程
pids=$(ps aux | grep "[p]ython.*startup.py -a" | awk '{print $2}')
if [ -n "$pids" ]; then
print_yellow "正在停止 startup.py 进程: $pids"
kill -9 $pids 2>/dev/null
print_green "startup.py 进程已停止"
fi
echo ""
print_yellow "=== 启动服务 ==="
cd /home/gc/gangyan/langchain-chat
# 初始化 conda确保 PATH 中包含 conda 路径)
CONDA_INIT="/root/miniconda3/etc/profile.d/conda.sh"
if [ -f "$CONDA_INIT" ]; then
# 初始化 conda将 conda 路径添加到 PATH
. "$CONDA_INIT" 2>/dev/null || source "$CONDA_INIT" 2>/dev/null
fi
# 查找 conda 可执行文件(按优先级)
if command -v conda &> /dev/null; then
CONDA_EXE="conda"
elif [ -f "/root/miniconda3/bin/conda" ]; then
CONDA_EXE="/root/miniconda3/bin/conda"
elif [ -f "/root/miniconda3/condabin/conda" ]; then
CONDA_EXE="/root/miniconda3/condabin/conda"
else
print_red "错误: 未找到 conda 命令"
print_red "请检查 conda 是否已安装: /root/miniconda3"
exit 1
fi
# 使用 conda 环境启动
print_yellow "使用环境: gangyan"
print_yellow "日志文件: nohup.out"
print_yellow "Conda路径: $CONDA_EXE"
# 获取 python 的完整路径
PYTHON_EXE="/root/miniconda3/envs/gangyan/bin/python"
if [ ! -f "$PYTHON_EXE" ]; then
# 尝试通过 conda run 获取路径
PYTHON_EXE="$($CONDA_EXE run -n gangyan which python 2>/dev/null)"
if [ -z "$PYTHON_EXE" ] || [ ! -f "$PYTHON_EXE" ]; then
print_red "错误: 无法找到 python 可执行文件"
print_red "请检查 conda 环境 gangyan 是否已安装"
exit 1
fi
fi
print_yellow "Python路径: $PYTHON_EXE"
# 直接使用 python 完整路径启动(不依赖 conda activate更可靠
# 设置 PYTHONPATH 确保能正确导入模块
export PYTHONPATH="/home/gc/gangyan/langchain-chat:$PYTHONPATH"
cd /home/gc/gangyan/langchain-chat
nohup "$PYTHON_EXE" startup.py -a >> nohup.out 2>&1 &
PID=$!
print_green "服务已启动PID: $PID"
print_yellow "日志文件: /home/gc/gangyan/langchain-chat/nohup.out"
print_yellow "查看日志: tail -f nohup.out"
# 等待几秒后显示日志
sleep 2
echo ""
print_yellow "=== 最近日志 ==="
tail -20 nohup.out