2025年1月19日Memo步骤 1:创建 Bash 脚本OpenWRTPVELinux搞了个定时脚本来解决 PVE 下 OpenWRT 意外卡死的问题。 这个 OpenWRT 容器作为网络中的唯一主路由,所以用最简单的方法就是直接在宿主机上检查网络联通性来推测 OpenWRT 是不是寄了。 定时任务使用了 systemed timer 来实现,感觉确实不错 -(Systemd 定时器教程 - 阮一峰的网络日志 pct(1) 步骤 1:创建 Bash 脚本 创建脚本文件 /usr/local/bin/restart_openwrt_when_error.sh: sudo nano /usr/local/bin/restart_openwrt_when_error.sh 将以下内容粘贴到脚本文件中: #!/bin/bash if [ -z "$1" ]; then echo -e "\e[31mUsage: $0 <LXC_ID>\e[0m" >&2 exit 1 fi LXC_ID=$1 RETRY_COUNT=3 RETRY_INTERVAL=5 fail_count=0 echo -e "\e[34mStarting network accessibility check for https://qq.com and https://baidu.com...\e[0m" for ((i=1; i<=RETRY_COUNT; i++)); do if curl -s --head --connect-timeout 10 https://qq.com/ > /dev/null || curl -s --head --connect-timeout 10 https://baidu.com/ > /dev/null; then echo -e "\e[32mNetwork is accessible.\e[0m" fail_count=0 break else展开展开全部 (39 行)#!/bin/bash if [ -z "$1" ]; then echo -e "\e[31mUsage: $0 <LXC_ID>\e[0m" >&2 exit 1 fi LXC_ID=$1 RETRY_COUNT=3 RETRY_INTERVAL=5 fail_count=0 echo -e "\e[34mStarting network accessibility check for https://qq.com and https://baidu.com...\e[0m" for ((i=1; i<=RETRY_COUNT; i++)); do if curl -s --head --connect-timeout 10 https://qq.com/ > /dev/null || curl -s --head --connect-timeout 10 https://baidu.com/ > /dev/null; then echo -e "\e[32mNetwork is accessible.\e[0m" fail_count=0 break else echo -e "\e[33mAttempt $i: Network is not accessible. Retrying in $RETRY_INTERVAL seconds...\e[0m" fail_count=$((fail_count + 1)) fi sleep $RETRY_INTERVAL done if [ $fail_count -eq $RETRY_COUNT ]; then echo -e "\e[31mNetwork is not accessible after $RETRY_COUNT attempts. Restarting LXC container $LXC_ID...\e[0m" >&2 if ! timeout 120 pct reboot $LXC_ID --timeout 30; then echo -e "\e[31mTimeout occurred during pct reboot. Forcing stop and start of LXC container $LXC_ID.\e[0m" >&2 pct stop $LXC_ID sleep 5 pct start $LXC_ID else echo -e "\e[32mLXC container $LXC_ID restarted successfully.\e[0m" fi else echo -e "\e[34mNo need to restart LXC container $LXC_ID.\e[0m" fi 收起收起 保存文件并退出编辑器。 赋予脚本执行权限: sudo chmod +x /usr/local/bin/restart_openwrt_when_error.sh 步骤 2:创建 Systemd 服务文件 创建服务文件 /etc/systemd/system/restart_openwrt_when_error.service: sudo nano /etc/systemd/system/restart_openwrt_when_error.service 添加以下内容: [Unit] Description=Check network access and restart OpenWRT LXC if unreachable [Service] Type=oneshot ExecStart=/usr/local/bin/restart_openwrt_when_error.sh 305 TimeoutStartSec=5min 将 <LXC_ID> 替换为实际的 LXC 容器 ID。 保存文件并退出编辑器。 步骤 3:创建 Systemd 定时器文件 创建定时器文件 /etc/systemd/system/restart_openwrt_when_error.timer: sudo nano /etc/systemd/system/restart_openwrt_when_error.timer 添加以下内容: [Unit] Description=Run restart_openwrt_when_error.service every 10 minutes [Timer] OnBootSec=3min OnUnitActiveSec=10min AccuracySec=1s Persistent=false [Install] WantedBy=timers.target 保存文件并退出编辑器。 步骤 4:启用并启动服务和定时器 重新加载 Systemd 配置: sudo systemctl daemon-reload 启用并启动定时器: sudo systemctl enable restart_openwrt_when_error.timer sudo systemctl start restart_openwrt_when_error.timer 检查定时器状态,以确保其正常运行: sudo systemctl status restart_openwrt_when_error.timer 步骤 5:查看日志 使用 journalctl 查看服务的日志输出,包括脚本的输出: journalctl -u restart_openwrt_when_error.service
步骤 1:创建 Bash 脚本
搞了个定时脚本来解决 PVE 下 OpenWRT 意外卡死的问题。
这个 OpenWRT 容器作为网络中的唯一主路由,所以用最简单的方法就是直接在宿主机上检查网络联通性来推测 OpenWRT 是不是寄了。
定时任务使用了 systemed timer 来实现,感觉确实不错
-(Systemd 定时器教程 - 阮一峰的网络日志
步骤 1:创建 Bash 脚本
/usr/local/bin/restart_openwrt_when_error.sh:sudo nano /usr/local/bin/restart_openwrt_when_error.sh#!/bin/bash if [ -z "$1" ]; then echo -e "\e[31mUsage: $0 <LXC_ID>\e[0m" >&2 exit 1 fi LXC_ID=$1 RETRY_COUNT=3 RETRY_INTERVAL=5 fail_count=0 echo -e "\e[34mStarting network accessibility check for https://qq.com and https://baidu.com...\e[0m" for ((i=1; i<=RETRY_COUNT; i++)); do if curl -s --head --connect-timeout 10 https://qq.com/ > /dev/null || curl -s --head --connect-timeout 10 https://baidu.com/ > /dev/null; then echo -e "\e[32mNetwork is accessible.\e[0m" fail_count=0 break else#!/bin/bash if [ -z "$1" ]; then echo -e "\e[31mUsage: $0 <LXC_ID>\e[0m" >&2 exit 1 fi LXC_ID=$1 RETRY_COUNT=3 RETRY_INTERVAL=5 fail_count=0 echo -e "\e[34mStarting network accessibility check for https://qq.com and https://baidu.com...\e[0m" for ((i=1; i<=RETRY_COUNT; i++)); do if curl -s --head --connect-timeout 10 https://qq.com/ > /dev/null || curl -s --head --connect-timeout 10 https://baidu.com/ > /dev/null; then echo -e "\e[32mNetwork is accessible.\e[0m" fail_count=0 break else echo -e "\e[33mAttempt $i: Network is not accessible. Retrying in $RETRY_INTERVAL seconds...\e[0m" fail_count=$((fail_count + 1)) fi sleep $RETRY_INTERVAL done if [ $fail_count -eq $RETRY_COUNT ]; then echo -e "\e[31mNetwork is not accessible after $RETRY_COUNT attempts. Restarting LXC container $LXC_ID...\e[0m" >&2 if ! timeout 120 pct reboot $LXC_ID --timeout 30; then echo -e "\e[31mTimeout occurred during pct reboot. Forcing stop and start of LXC container $LXC_ID.\e[0m" >&2 pct stop $LXC_ID sleep 5 pct start $LXC_ID else echo -e "\e[32mLXC container $LXC_ID restarted successfully.\e[0m" fi else echo -e "\e[34mNo need to restart LXC container $LXC_ID.\e[0m" fi保存文件并退出编辑器。
赋予脚本执行权限:
sudo chmod +x /usr/local/bin/restart_openwrt_when_error.sh步骤 2:创建 Systemd 服务文件
/etc/systemd/system/restart_openwrt_when_error.service:sudo nano /etc/systemd/system/restart_openwrt_when_error.service[Unit] Description=Check network access and restart OpenWRT LXC if unreachable [Service] Type=oneshot ExecStart=/usr/local/bin/restart_openwrt_when_error.sh 305 TimeoutStartSec=5min将
<LXC_ID>替换为实际的 LXC 容器 ID。步骤 3:创建 Systemd 定时器文件
/etc/systemd/system/restart_openwrt_when_error.timer:sudo nano /etc/systemd/system/restart_openwrt_when_error.timer[Unit] Description=Run restart_openwrt_when_error.service every 10 minutes [Timer] OnBootSec=3min OnUnitActiveSec=10min AccuracySec=1s Persistent=false [Install] WantedBy=timers.target步骤 4:启用并启动服务和定时器
sudo systemctl daemon-reloadsudo systemctl enable restart_openwrt_when_error.timer sudo systemctl start restart_openwrt_when_error.timersudo systemctl status restart_openwrt_when_error.timer步骤 5:查看日志
journalctl查看服务的日志输出,包括脚本的输出: