Files
WPSBot/deploy/manage.sh
2025-10-28 13:00:35 +08:00

165 lines
3.5 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
# WPS Bot Game 服务管理脚本
SERVICE_NAME="wps-bot"
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# 显示帮助信息
show_help() {
echo "WPS Bot Game 服务管理工具"
echo ""
echo "用法: $0 {start|stop|restart|status|logs|enable|disable|update}"
echo ""
echo "命令说明:"
echo " start - 启动服务"
echo " stop - 停止服务"
echo " restart - 重启服务"
echo " status - 查看服务状态"
echo " logs - 查看实时日志"
echo " enable - 启用开机自启"
echo " disable - 禁用开机自启"
echo " update - 更新代码并重启"
echo ""
}
# 启动服务
start_service() {
echo -e "${YELLOW}正在启动服务...${NC}"
sudo systemctl start ${SERVICE_NAME}
if [ $? -eq 0 ]; then
echo -e "${GREEN}✅ 服务启动成功${NC}"
sleep 2
sudo systemctl status ${SERVICE_NAME} --no-pager
else
echo -e "${RED}❌ 服务启动失败${NC}"
exit 1
fi
}
# 停止服务
stop_service() {
echo -e "${YELLOW}正在停止服务...${NC}"
sudo systemctl stop ${SERVICE_NAME}
if [ $? -eq 0 ]; then
echo -e "${GREEN}✅ 服务已停止${NC}"
else
echo -e "${RED}❌ 服务停止失败${NC}"
exit 1
fi
}
# 重启服务
restart_service() {
echo -e "${YELLOW}正在重启服务...${NC}"
sudo systemctl restart ${SERVICE_NAME}
if [ $? -eq 0 ]; then
echo -e "${GREEN}✅ 服务重启成功${NC}"
sleep 2
sudo systemctl status ${SERVICE_NAME} --no-pager
else
echo -e "${RED}❌ 服务重启失败${NC}"
exit 1
fi
}
# 查看状态
show_status() {
sudo systemctl status ${SERVICE_NAME}
}
# 查看日志
show_logs() {
echo -e "${YELLOW}实时日志按Ctrl+C退出:${NC}"
sudo journalctl -u ${SERVICE_NAME} -f
}
# 启用开机自启
enable_service() {
echo -e "${YELLOW}启用开机自启...${NC}"
sudo systemctl enable ${SERVICE_NAME}
if [ $? -eq 0 ]; then
echo -e "${GREEN}✅ 已启用开机自启${NC}"
else
echo -e "${RED}❌ 启用失败${NC}"
exit 1
fi
}
# 禁用开机自启
disable_service() {
echo -e "${YELLOW}禁用开机自启...${NC}"
sudo systemctl disable ${SERVICE_NAME}
if [ $? -eq 0 ]; then
echo -e "${GREEN}✅ 已禁用开机自启${NC}"
else
echo -e "${RED}❌ 禁用失败${NC}"
exit 1
fi
}
# 更新代码
update_service() {
echo -e "${YELLOW}正在更新代码...${NC}"
# 停止服务
stop_service
# 进入项目目录
cd /opt/wps-bot
# 拉取最新代码如果使用git
if [ -d ".git" ]; then
echo -e "${YELLOW}从Git拉取最新代码...${NC}"
sudo -u ubuntu git pull
fi
# 更新依赖
echo -e "${YELLOW}更新依赖...${NC}"
sudo -u ubuntu /home/ubuntu/miniconda3/envs/liubai/bin/pip install -r requirements.txt
# 重启服务
start_service
echo -e "${GREEN}✅ 更新完成${NC}"
}
# 主逻辑
case "$1" in
start)
start_service
;;
stop)
stop_service
;;
restart)
restart_service
;;
status)
show_status
;;
logs)
show_logs
;;
enable)
enable_service
;;
disable)
disable_service
;;
update)
update_service
;;
*)
show_help
exit 1
;;
esac
exit 0