OpenWrt自动重启配置

清夏晚风

OpenWrt自动重启配置

概述

OpenWrt作为功能强大的开源路由器系统,在长时间运行后可能会出现内存泄漏、连接数过多等问题,导致系统性能下降。配置定时自动重启可以有效解决这些问题,保持路由器的最佳运行状态。

为什么需要定时重启

常见问题

  • 内存泄漏:长时间运行后可用内存逐渐减少
  • 连接数堆积:TCP连接未及时释放,影响新连接建立
  • DHCP租约问题:租约分配异常导致IP冲突
  • 无线信号衰减:无线模块长期工作导致的性能下降
  • 系统缓存堆积:各种系统缓存未及时清理

重启的好处

  • 释放被占用的内存资源
  • 清理无效的网络连接
  • 重置系统状态,恢复性能
  • 应用系统更新和配置变更
  • 延长硬件使用寿命

基础配置方法

方法一:使用Crontab定时任务

OpenWrt使用cron服务来执行定时任务,这是最常见和推荐的方法。

步骤1:编辑crontab文件

1
2
3
4
5
# 使用vi编辑器打开crontab文件
crontab -e

# 或者编辑系统crontab文件
vi /etc/crontabs/root

步骤2:添加定时重启任务

每天凌晨2点重启

1
0 2 * * * sleep 5 && touch /etc/banner && reboot

每天凌晨5点10分重启

1
10 5 * * * sleep 70 && touch /etc/banner && reboot

每周一凌晨3点重启

1
0 3 * * 1 sleep 5 && touch /etc/banner && reboot

步骤3:重启cron服务

1
2
3
/etc/init.d/cron restart
# 或者
service cron restart

方法二:使用LuCI Web界面配置

对于不熟悉命令行的用户,可以通过Web界面配置:

  1. 登录OpenWrt管理界面
  2. 导航到”系统” → “计划任务”
  3. 在文本框中添加定时任务
  4. 点击”保存并应用”

高级配置技巧

自动校时配置

重启前自动校时是非常重要的,可以避免因时间不准确导致的无限重启问题。

安装NTP客户端

1
2
opkg update
opkg install ntpd

配置NTP服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 编辑NTP配置文件
vi /etc/config/system

# 添加或修改以下内容
config system
option hostname 'OpenWrt'
option timezone 'CST-8'
option zonename 'Asia/Shanghai'

config timeserver 'ntp'
list server 'ntp.aliyun.com'
list server 'cn.pool.ntp.org'
list server 'time.google.com'
option enabled '1'
option enable_server '0'

完整的定时重启配置

1
2
3
4
5
# 每天凌晨2点重启,包含校时
0 2 * * * /usr/sbin/ntpd -q -p ntp.aliyun.com && sleep 5 && touch /etc/banner && reboot

# 或者更详细的配置
0 2 * * * sleep 70 && /usr/sbin/ntpd -q -p ntp.aliyun.com && touch /etc/banner && reboot

智能重启策略

基于系统负载的重启

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 创建智能重启脚本
vi /usr/bin/smart_reboot.sh

#!/bin/sh
# 获取系统负载
load_avg=$(uptime | awk -F'load average:' '{print $2}' | awk '{print $1}' | sed 's/,//')
# 获取内存使用率
mem_usage=$(free | grep Mem | awk '{printf "%.0f", $3/$2 * 100}')

# 如果负载超过3或内存使用率超过90%,则重启
if [ $(echo "$load_avg > 3.0" | bc) -eq 1 ] || [ $mem_usage -gt 90 ]; then
logger "System load or memory usage too high, rebooting..."
sleep 5 && reboot
fi

基于网络连接数的重启

1
2
3
4
5
6
7
8
9
10
11
12
# 创建连接数检查脚本
vi /usr/bin/conn_check.sh

#!/bin/sh
# 获取当前连接数
conn_count=$(netstat -an | grep ESTABLISHED | wc -l)

# 如果连接数超过1000,则重启
if [ $conn_count -gt 1000 ]; then
logger "Too many connections ($conn_count), rebooting..."
sleep 5 && reboot
fi

常见问题解决

问题1:无限重启循环

现象:路由器不断重启,无法正常启动

原因

  • 系统时间未正确同步
  • 重启条件设置不当
  • 硬件时钟问题

解决方案

1
2
3
4
5
6
7
8
9
10
11
12
# 1. 进入安全模式或failsafe模式
# 2. 挂载文件系统为可写
mount_root

# 3. 编辑crontab文件,删除或注释重启任务
vi /etc/crontabs/root

# 4. 同步系统时间
ntpdate -s ntp.aliyun.com

# 5. 重启系统
reboot -f

问题2:重启后时间重置

现象:每次重启后时间都回到默认值

解决方案

1
2
3
4
5
6
7
8
9
10
11
# 安装并配置hwclock
opkg install busybox-hwclock

# 保存当前时间到硬件时钟
hwclock -w

# 在启动脚本中添加时间同步
vi /etc/rc.local
# 添加以下内容
/usr/sbin/ntpd -q -p ntp.aliyun.com
hwclock -w

问题3:定时任务不执行

现象:设置的定时重启任务没有执行

排查步骤

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 1. 检查cron服务状态
/etc/init.d/cron status

# 2. 检查crontab语法
crontab -l

# 3. 查看系统日志
grep CRON /var/log/messages

# 4. 检查文件权限
ls -la /etc/crontabs/

# 5. 手动测试重启命令
touch /etc/banner && reboot

问题4:重启后配置丢失

现象:重启后某些配置恢复为默认值

解决方案

1
2
3
4
5
6
7
8
9
10
11
# 1. 确保配置已保存
uci commit

# 2. 检查overlay文件系统
mount | grep overlay

# 3. 手动保存配置
cp -r /overlay/upper/etc /etc

# 4. 检查磁盘空间
df -h

最佳实践建议

重启时间选择

  • 推荐时间:凌晨2:00-5:00,网络使用低谷期
  • 避免时间:18:00-23:00,家庭网络使用高峰期
  • 考虑因素:用户作息习惯、网络负载、维护窗口

重启频率建议

设备类型 重启频率 推荐时间
家用路由器 每周1次 凌晨3:00
企业路由器 每月1次 周末凌晨
高负载设备 每周2次 凌晨2:30
低负载设备 每月1次 凌晨4:00

监控与日志

1
2
3
4
5
6
7
8
# 启用系统日志
uci set system.@system[0].log_file='/var/log/messages'
uci set system.@system[0].log_size='10240'
uci set system.@system[0].log_remote='0'
uci commit system

# 创建重启日志记录
echo "0 2 * * * logger 'System auto reboot starting...' && sleep 5 && touch /etc/banner && reboot" >> /etc/crontabs/root

备份策略

1
2
3
4
5
6
7
8
# 创建自动备份脚本
vi /usr/bin/auto_backup.sh

#!/bin/sh
# 备份配置文件
tar -czf /tmp/config_backup_$(date +%Y%m%d_%H%M%S).tar.gz /etc/config
# 保留最近7天的备份
find /tmp -name "config_backup_*.tar.gz" -mtime +7 -delete

高级功能

1. 智能重启系统

创建一个综合的智能重启系统:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
vi /usr/bin/intelligent_reboot.sh

#!/bin/sh
# 智能重启判断脚本

# 获取系统指标
load_avg=$(uptime | awk -F'load average:' '{print $2}' | awk '{print $1}' | sed 's/,//')
mem_usage=$(free | grep Mem | awk '{printf "%.0f", $3/$2 * 100}')
conn_count=$(netstat -an | grep ESTABLISHED | wc -l)
uptime_days=$(cat /proc/uptime | awk '{printf "%.0f", $1/86400}')

# 计算重启评分
score=0

# 负载评分
if [ $(echo "$load_avg > 2.0" | bc) -eq 1 ]; then
score=$((score + 30))
elif [ $(echo "$load_avg > 1.0" | bc) -eq 1 ]; then
score=$((score + 15))
fi

# 内存评分
if [ $mem_usage -gt 90 ]; then
score=$((score + 25))
elif [ $mem_usage -gt 80 ]; then
score=$((score + 15))
fi

# 连接数评分
if [ $conn_count -gt 500 ]; then
score=$((score + 20))
elif [ $conn_count -gt 200 ]; then
score=$((score + 10))
fi

# 运行时间评分
if [ $uptime_days -gt 30 ]; then
score=$((score + 25))
elif [ $uptime_days -gt 14 ]; then
score=$((score + 15))
fi

# 记录日志
logger "System health score: $score (load: $load_avg, mem: $mem_usage%, conn: $conn_count, uptime: $uptime_days days)"

# 如果评分超过阈值,执行重启
if [ $score -gt 70 ]; then
logger "System health score too high ($score), initiating reboot..."
echo "System will reboot in 5 minutes due to health issues" | wall
sleep 300 && reboot
fi

2. 条件重启配置

在crontab中添加条件重启:

1
2
3
4
5
# 每小时检查系统状态,必要时重启
0 * * * * /usr/bin/intelligent_reboot.sh

# 每天凌晨检查并重置计数器
0 0 * * * echo "0" > /tmp/reboot_counter

3. 网络唤醒集成

如果需要远程唤醒设备:

1
2
3
4
5
6
7
8
9
# 安装ethtool
opkg install ethtool

# 启用网络唤醒
ethtool -s eth0 wol g

# 在启动脚本中添加
vi /etc/rc.local
# 添加:ethtool -s eth0 wol g

总结

OpenWrt的自动重启配置是系统维护的重要组成部分,合理配置可以有效提升系统稳定性和性能。通过本文的详细介绍,您应该已经掌握了:

  1. 基础配置方法:crontab和Web界面配置
  2. 高级技巧:自动校时、智能重启策略
  3. 问题排查:常见问题的解决方案
  4. 最佳实践:时间选择、频率建议、监控方法

记住,定时重启只是系统维护的一部分,还需要配合其他优化措施,如定期清理日志、更新固件、监控性能等,才能确保路由器长期稳定运行。

重要提醒

  • 配置前务必备份重要数据
  • 测试配置时建议先在非生产环境验证
  • 保持系统时间准确是避免问题的关键
  • 定期检查和维护重启配置

通过合理的自动重启配置,您的OpenWrt路由器将能够长期保持稳定、高效的运行状态。

  • Title: OpenWrt自动重启配置
  • Author: 清夏晚风
  • Created at : 2026-03-02 09:57:27
  • Updated at : 2026-03-14 16:00:06
  • Link: https://blog.kimikkorow.eu.org/OpenWrt自动重启配置/
  • License: This work is licensed under CC BY-NC-SA 4.0.