由于openwrt有时会抽风重启,特编写一个脚本判断通过uptime判断当前系统是否有重启过,并通过http发送通知给onebot等提醒自己。
脚本内容如下:
#!/bin/sh uptime_file=/root/uptime if [ ! -f $uptime_file ];then echo 0 > $uptime_file fi uptime=`ubus call system info | sed 's/,/\n/g' | grep "uptime" | sed 's/:/\n/g' | sed '1d' | sed 's/"//g'` if [ $uptime -gt 0 ];then old_time=`cat $uptime_file` if [ $uptime -lt $old_time ];then echo "the device was reboot at $uptime s ago." curl -H "Authorization:xxx" -H "xxx:xxx" -H "Content-Type: application/json" -d '{"user_id":"xxx","message":"the device was reboot at '"$uptime"'s ago,the device was running '$old_time's."}' -X POST https://xxx/send_private_msg > result.txt result=`cat result.txt | sed 's/,/\n/g' | grep "retcode" | sed 's/:/\n/g' | sed '1d' | sed 's/"//g'` if [ $result == 0 ];then echo $uptime > $uptime_file rm -f result.txt else echo 'the curl not success.' fi else echo $uptime > $uptime_file echo "the device is run in $uptime s." fi fi
修改url中的xxx为自己onebot的个人信息后将其加入到openwrt的定时任务中即可。
推荐定时为每分钟执行一次,即在定时任务中添加以下一行:
* * * * * /root/check_uptime.sh
原理:
定时执行该脚本,获取当前系统已运行时间并保存到/root/uptime文件中,当系统重启后通过读取该文件中的值与当前已运行时间对比,若大于当前已运行时间,则表示系统重启过,发送通知给onebot,正常运行则更新uptime文件中的值。
注意:
1.请确保/root目录可读写且空间足够
2.请修改url中的xxx值为自己搭建的onebot服务信息
3.请确保ubus call system info能获取到系统的uptime字段
4.通知onebot为QQ机器人的node程序,可自行改为其他通知