使用方法:

1.webp-1
  1. 将脚本上传到服务器
  2. 运行命令:chmod +x bt_daily_restart.sh
  3. 执行脚本:./bt_daily_restart.sh

✅ 脚本功能:

  • 自动检测宝塔面板环境
  • 创建安全的服务重启脚本
  • 设置每天凌晨2点自动重启
  • 自动配置cron定时任务
  • 完整的日志记录和验证

🔧 配置完成后:

  • 重启时间:每天凌晨 02:00
  • 重启日志:/var/log/bt_reboot.log
  • 脚本路径:/opt/bt_restart/restart_server.sh

📋 管理命令:
查看定时任务:crontab -l
查看重启日志:tail -f /var/log/bt_reboot.log
手动执行重启:/opt/bt_restart/restart_server.sh
删除定时任务:crontab -l | grep -v ‘/opt/bt_restart/restart_server.sh’ | crontab –

⚠️ 注意事项:

  • 请使用root权限执行
  • 确保服务器已安装宝塔面板
  • 建议在低访问量时段使用
  • 定期检查日志确保正常运行
=========================================================
#!/bin/bash
# 宝塔服务器每天凌晨2点自动重启一键配置脚本
# 运行此脚本即可自动配置每天凌晨2点重启服务器服务
# 配置变量
SCRIPT_NAME=“bt_restart”
INSTALL_DIR=“/opt/$SCRIPT_NAME”
RESTART_SCRIPT=“$INSTALL_DIR/restart_server.sh”
LOG_FILE=“/var/log/bt_daily_restart.log”
CRON_TIME=“0 2 * * *”
# 颜色输出
RED=‘33[0;31m’
GREEN=‘33[0;32m’
YELLOW=‘33[1;33m’
BLUE=‘33[0;34m’
NC=‘33[0m’
# 清屏
clear
# 显示标题
echo -e “${BLUE}================================================${NC}”
echo -e “${BLUE} 宝塔服务器每天凌晨2点自动重启配置工具${NC}”
echo -e “${BLUE} 趣九游源码资源网www.qujiuyou.com${NC}”
echo -e “${BLUE} QQ:88468818{NC}”
echo -e “${BLUE}================================================${NC}”
echo “”
# 日志函数
log() {
echo “$(date ‘+%Y-%m-%d %H:%M:%S’) – $1” | tee -a “$LOG_FILE”
}
print_info() {
echo -e “${GREEN}[INFO]${NC} $1”
log “[INFO] $1”
}
print_warning() {
echo -e “${YELLOW}[WARNING]${NC} $1”
log “[WARNING] $1”
}
print_error() {
echo -e “${RED}[ERROR]${NC} $1”
log “[ERROR] $1”
}
# 检查是否为root用户
check_root() {
if [ “$EUID” -ne 0 ]; then
print_error “请使用root权限运行此脚本”
echo “使用命令: sudo $0”
exit 1
fi
print_info “Root权限检查通过”
}
# 检查系统
check_system() {
print_info “检查系统环境…”
# 检查操作系统
if [ -f /etc/os-release ]; then
. /etc/os-release
print_info “操作系统: $PRETTY_NAME”
fi
# 检查宝塔面板
if [ -f “/etc/init.d/bt” ]; then
print_info “检测到宝塔面板已安装”
bt_version=$(bt info | grep “面板版本” | awk ‘{print $2}’ 2>/dev/null)
if [ ! -z “$bt_version” ]; then
print_info “宝塔面板版本: $bt_version”
fi
else
print_error “未检测到宝塔面板,请先安装宝塔面板”
exit 1
fi
# 检查cron服务
if systemctl is-active –quiet cron 2>/dev/null || systemctl is-active –quiet crond 2>/dev/null; then
print_info “定时任务服务运行正常”
else
print_warning “定时任务服务未运行,将自动启动”
systemctl start cron 2>/dev/null || systemctl start crond 2>/dev/null
systemctl enable cron 2>/dev/null || systemctl enable crond 2>/dev/null
print_info “定时任务服务已启动”
fi
}
# 创建重启脚本
create_restart_script() {
print_info “创建服务器重启脚本…”
# 创建安装目录
mkdir -p “$INSTALL_DIR”
# 创建重启脚本内容
cat > “$RESTART_SCRIPT” << ‘EOF’
#!/bin/bash
# 宝塔服务器重启脚本 – 自动执行时间: $(date)
LOG_FILE=“/var/log/bt_reboot.log”
# 日志函数
log() {
echo “$(date ‘+%Y-%m-%d %H:%M:%S’) – $1” | tee -a “$LOG_FILE”
}
print_info() {
echo -e “33[0;32m[INFO]33[0m $1”
log “[INFO] $1”
}
print_error() {
echo -e “33[0;31m[ERROR]33[0m $1”
log “[ERROR] $1”
}
# 停止服务
stop_services() {
print_info “开始停止服务…”
# 停止Nginx
if systemctl is-active –quiet nginx 2>/dev/null; then
systemctl stop nginx
print_info “Nginx已停止”
fi
# 停止Apache
if systemctl is-active –quiet httpd 2>/dev/null; then
systemctl stop httpd
print_info “Apache已停止”
fi
# 停止MySQL
if systemctl is-active –quiet mysqld 2>/dev/null || systemctl is-active –quiet mysql 2>/dev/null; then
systemctl stop mysqld 2>/dev/null || systemctl stop mysql 2>/dev/null
print_info “MySQL已停止”
fi
# 停止PHP-FPM
for php_version in 56 70 71 72 73 74 80 81 82; do
if systemctl is-active –quiet php-fpm-${php_version} 2>/dev/null; then
systemctl stop php-fpm-${php_version}
print_info “PHP-FPM ${php_version}已停止”
fi
done
# 停止Redis
if systemctl is-active –quiet redis 2>/dev/null || systemctl is-active –quiet redis-server 2>/dev/null; then
systemctl stop redis 2>/dev/null || systemctl stop redis-server 2>/dev/null
print_info “Redis已停止”
fi
# 停止宝塔面板
/etc/init.d/bt stop 2>/dev/null
print_info “宝塔面板已停止”
}
# 启动服务
start_services() {
print_info “开始启动服务…”
# 启动宝塔面板
/etc/init.d/bt start 2>/dev/null
sleep 5
print_info “宝塔面板已启动”
# 启动MySQL
systemctl start mysqld 2>/dev/null || systemctl start mysql 2>/dev/null
sleep 10
print_info “MySQL已启动”
# 启动Redis
if systemctl list-unit-files | grep -q “redis.service|redis-server.service”; then
systemctl start redis 2>/dev/null || systemctl start redis-server 2>/dev/null
print_info “Redis已启动”
fi
# 启动PHP-FPM
for php_version in 56 70 71 72 73 74 80 81 82; do
if systemctl list-unit-files | grep -q “php-fpm-${php_version}.service”; then
systemctl start php-fpm-${php_version}
print_info “PHP-FPM ${php_version}已启动”
fi
done
# 启动Nginx
if systemctl list-unit-files | grep -q “nginx.service”; then
systemctl start nginx
print_info “Nginx已启动”
fi
# 启动Apache
if systemctl list-unit-files | grep -q “httpd.service”; then
systemctl start httpd
print_info “Apache已启动”
fi
}
# 主执行流程
print_info “===== 开始服务器重启 =====”
# 停止所有服务
stop_services
# 清理临时文件
find /tmp -type f -atime +7 -delete 2>/dev/null
print_info “临时文件清理完成”
# 等待30秒
sleep 30
# 启动所有服务
start_services
# 等待服务稳定
sleep 20
print_info “===== 服务器重启完成 =====”
EOF
# 设置执行权限
chmod +x “$RESTART_SCRIPT”
print_info “重启脚本创建完成: $RESTART_SCRIPT”
}
# 设置定时任务
setup_cron_job() {
print_info “设置定时任务 – 每天凌晨2点重启…”
# 删除已存在的相同任务
crontab -l 2>/dev/null | grep -v “$RESTART_SCRIPT” | crontab –
# 添加新的定时任务
(crontab -l 2>/dev/null; echo “$CRON_TIME $RESTART_SCRIPT >> /var/log/bt_reboot.log 2>&1”) | crontab –
if [ $? -eq 0 ]; then
print_info “定时任务设置成功!”
print_info “执行时间: 每天凌晨 02:00”
else
print_error “定时任务设置失败”
return 1
fi
}
# 验证配置
verify_config() {
print_info “验证配置…”
# 检查脚本文件
if [ -f “$RESTART_SCRIPT” ] && [ -x “$RESTART_SCRIPT” ]; then
print_info “✅ 重启脚本正常”
else
print_error “❌ 重启脚本异常”
return 1
fi
# 检查定时任务
if crontab -l 2>/dev/null | grep -q “$RESTART_SCRIPT”; then
print_info “✅ 定时任务正常”
else
print_error “❌ 定时任务异常”
return 1
fi
print_info “配置验证完成”
}
# 显示配置信息
show_config() {
echo “”
echo -e “${GREEN}🎉 配置完成!${NC}”
echo “”
echo -e “${BLUE}配置信息:${NC}”
echo “⏰ 重启时间: 每天凌晨 02:00”
echo “📜 脚本路径: $RESTART_SCRIPT”
echo “📝 重启日志: /var/log/bt_reboot.log”
echo “🔧 配置日志: $LOG_FILE”
echo “”
echo -e “${BLUE}常用命令:${NC}”
echo “查看定时任务: crontab -l”
echo “查看重启日志: tail -f /var/log/bt_reboot.log”
echo “手动执行重启: $RESTART_SCRIPT”
echo “删除定时任务: crontab -l | grep -v ‘$RESTART_SCRIPT’ | crontab -“
echo “”
echo -e “${BLUE}注意事项:${NC}”
echo “• 建议在访问量较低的时间段(凌晨2点)重启”
echo “• 定期检查重启日志确保正常运行”
echo “• 重启前重要数据请提前备份”
echo “”
echo -e “${YELLOW}如需修改重启时间,请编辑crontab任务${NC}”
echo “”
}
# 主函数
main() {
print_info “开始配置宝塔服务器定时重启…”
# 检查root权限
check_root
# 检查系统环境
check_system
# 创建重启脚本
create_restart_script
# 设置定时任务
setup_cron_job
# 验证配置
verify_config
# 显示配置信息
show_config
print_info “配置完成!服务器将在每天凌晨2点自动重启。”
}
# 执行主函数
main “$@”

=========================================================

 

创建的文件
bt_daily_restart.sh – 主配置脚本(一键配置)
使用说明.txt – 简化的使用说明
🚀 使用方法

  1. 上传到服务器
    将 bt_daily_restart.sh 上传到服务器任意目录,推荐 /root/
  2. 设置执行权限并运行
chmod +x bt_daily_restart.sh
./bt_daily_restart.sh

脚本会自动完成
✅ 检查宝塔面板环境
✅ 创建安全的服务重启脚本
✅ 设置每天凌晨2:00自动重启
✅ 配置cron定时任务
✅ 验证配置是否成功
✅ 提供完整的管理命令
📋 配置后信息
重启时间:每天凌晨 02:00
重启脚本: /opt/bt_restart/restart_server.sh
重启日志: /var/log/bt_reboot.log
配置日志: /var/log/bt_daily_restart.log

常用的管理命令

# 查看定时任务
crontab -l
# 查看重启日志
tail -f /var/log/bt_reboot.log
# 手动执行重启
/opt/bt_restart/restart_server.sh
# 删除定时任务
crontab -l | grep -v ‘/opt/bt_restart/restart_server.sh’ | crontab –

运行 ./bt_daily_restart.sh 即可完成所有配置,服务器将在每天凌晨2点自动重启所有宝塔相关服务!