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

95 lines
3.0 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
#uwsgi快速启动脚本
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
if [ $(whoami) != "root" ];then
echo "请使用root权限执行本启动命令"
exit 1;
fi
# 获取当前脚本文件的目录(一定要放到项目根目录中)
PROJECT_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
UWSGI_COMMOND="${PROJECT_ROOT}/venv/bin/uwsgi"
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 ""
}
echo -e "============================================"
echo -e "1、检查项目python虚拟环境是否存在"
echo -e "============================================"
if [ -d "${PROJECT_ROOT}/venv" ]; then
Print_Success_Text "项目Python虚拟环境已安装"
sleep 1
else
Return_Error "ERROR: 项目Python虚拟环境未安装,请先执行install.sh脚本" "命令示例sh install.sh"
exit 1
fi
if [ -x "${UWSGI_COMMOND}" ]; then
Print_Success_Text "检测到uwsgi已安装"
sleep 1
else
Return_Error "ERROR: 未在项目虚拟环境中检测到uwsgi请您确保是否正确执行了install.sh脚本"
exit 1
fi
echo -e "============================================"
echo -e "2、检查本项目是否存在uwsgi服务"
echo -e "============================================"
serviceCheck=$(ps aux | grep ${UWSGI_COMMOND} | grep -v grep)
if [ -z "${serviceCheck}" ]; then
Show_Text_With_Ellipsis "检测到uwsgi服务未运行正在启动" 0.3
else
echo "${serviceCheck}"
Show_Text_With_Ellipsis "正在关闭项目uwsgi服务" 0.3
# 使用 pgrep 找到与特定命令匹配的进程ID
pids=$(pgrep -f "$UWSGI_COMMOND")
if [ -n "$pids" ]; then
# 循环遍历结果中的每个进程ID并使用 kill 命令终止进程
for pid in $pids; do
kill -9 "$pid"
echo "已成功终止进程: $pid"
done
fi
Show_Text_With_Ellipsis "正在启动uwsgi服务" 0.3
fi
if [ ! -d "$PWD/logs" ]; then
mkdir "$PWD/logs"
fi
nohup ${UWSGI_COMMOND} --ini "${PROJECT_ROOT}/uwsgi_config.ini" --set project_dir="${PROJECT_ROOT}" >> ./logs/uwsgi_nohup.log 2>&1 & #后台启动服务将日志写入uwsgi_nohup.log文件
sleep 1
runerviceCheck=$(ps aux | grep ${UWSGI_COMMOND} | grep -v grep)
if [ -z "${runerviceCheck}" ]; then
Return_Error "启动uwsgi服务失败请检查日志文件"
else
Print_Success_Text "uwsgi服务启动成功"
echo "${runerviceCheck}"
fi