dvlyadmin_pro/backend/gunicorn_restart.sh
2025-03-18 08:46:50 +08:00

124 lines
3.9 KiB
Bash
Raw Permalink 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
#author:lybbn
#qq:1042594286
#version:1.1
#EditDate:2024-02-19
#gunicorn快速重启/关闭脚本
#sh gunicorn_restart.sh stop 仅关闭gunicorn服务
#sh gunicorn_restart.sh status 仅查看gunicorn服务状态
#sh gunicorn_restart.sh 重启gunicorn服务
clear
PATH=/www/server/pyporject_evn/lyadmin_venv/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
LANG=en_US.UTF-8
Venvpath="/www/server/pyporject_evn/lyadmin_venv"
if [ $(whoami) != "root" ];then
echo "请使用root权限执行本启动命令"
exit 1;
fi
# 获取当前脚本文件的目录(一定要放到项目根目录中)
PROJECT_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
GUNICORN_COMMOND="${Venvpath}/bin/gunicorn"
Return_Error(){
echo '=================================================';
printf '\033[1;31;40m%b\033[0m\n' "$@";
exit 1;
}
# 参数:$1 - 要输出的文本
Print_Success_Text() {
GREEN="\033[32m"
RESET="\033[0m"
echo -e "${GREEN}$1${RESET}"
}
# 参数:$1 - 要逐个显示的文本
# $2 - 控制字符输出速度的暂停时间(可选,默认为 0.2 秒)
Show_Text_With_Ellipsis() {
text="$1"
pause=${2:-0.1}
echo -n "$text"
sleep 1 # 显示 "这是一个例子" 1秒钟
for ((i=0; i<3; i++)); do
echo -n "."
sleep $pause
done
echo ""
}
#关闭gunicorn服务
Stop_Service() {
# 使用 pgrep 找到与特定命令匹配的进程ID
pids=$(pgrep -f "$GUNICORN_COMMOND")
if [ -n "$pids" ]; then
# 循环遍历结果中的每个进程ID并使用 kill 命令终止进程
for pid in $pids; do
kill -9 "$pid"
echo "已成功终止进程: $pid"
done
fi
}
echo -e "================================================="
echo -e "1、检查项目python虚拟环境是否存在"
echo -e "================================================="
if [ -d "${Venvpath}" ]; then
Print_Success_Text "项目Python虚拟环境已安装"
sleep 1
else
Return_Error "ERROR: 项目Python虚拟环境未安装,请先执行install.sh脚本" "命令示例sh install.sh"
exit 1
fi
if [ -x "${GUNICORN_COMMOND}" ]; then
Print_Success_Text "检测到gunicorn已安装"
sleep 1
else
Return_Error "ERROR: 未在项目虚拟环境中检测到gunicorn请您确保是否正确执行了install.sh脚本"
exit 1
fi
echo -e "================================================="
echo -e "2、检查本项目是否存在gunicorn服务"
echo -e "================================================="
serviceCheck=$(ps aux | grep ${GUNICORN_COMMOND} | grep -v grep)
echo "${serviceCheck}"
if [ $# -ge 1 ] && [ "$1" = "stop" ]; then
# 如果第一个参数为stop为仅关闭操作则只关闭服务
if [ -z "${serviceCheck}" ]; then
Show_Text_With_Ellipsis "检测到gunicorn服务未运行" 0.3
exit 1
else
Show_Text_With_Ellipsis "正在关闭项目gunicorn服务" 0.3
Stop_Service
exit 1
fi
elif [ $# -ge 1 ] && [ "$1" = "status" ]; then
if [ -z "${serviceCheck}" ]; then
Return_Error "检测到gunicorn服务未运行"
exit 1
else
Print_Success_Text "检测到gunicorn服务已运行"
exit 1
fi
else
if [ -z "${serviceCheck}" ]; then
Show_Text_With_Ellipsis "检测到gunicorn服务未运行正在启动" 0.3
else
Show_Text_With_Ellipsis "正在关闭项目gunicorn服务" 0.3
Stop_Service
Show_Text_With_Ellipsis "正在启动gunicorn服务" 0.3
fi
fi
project_logs_path="${PROJECT_ROOT}/logs"
if [ ! -d "$project_logs_path" ]; then
mkdir -p "$project_logs_path"
fi
nohup ${GUNICORN_COMMOND} -c gunicorn_config.py application.asgi:application >> ./logs/gunicorn_nohup.log 2>&1 & #后台启动服务将日志写入gunicorn_nohup.log文件
sleep 1
Print_Success_Text "gunicorn服务启动成功"
ps aux | grep "${GUNICORN_COMMOND}" | grep -v grep