- 1:脚本编辑
- 2:开机自启
脚本编辑
业务需求:软路由安装PVE或者ESXi后,遭遇停电或者突然非常关机,导致机器无法启动,其恢复需要花费一定时间。
前提条件:需要jd或tb购买一个一百来块的小充电宝或者UPS间接给软路由供电
因此有如下思路:使得软路由PVE系统或者ESXi系统,PVE系统本质上为Linux系统,让其间隔一定时间ping下游网络设备,如家里的摄像头ip、路由器ip等(下游IP需要是静态IP,且长时间运行,市电供电。
改进(2021年09月30日):原思路PVE系统内部ping下游网络设备,PVE自动关机时,虚拟的openwrt被强制关机,有概率出现openwrt崩溃。改进思路:openwrt访问下游网络设备状况,下游设备访问失败,openwrt先行关机,然后PVE在访问openwrt,如果访问失败,最后再关PVE,则保证关机先后顺序。
step1: 在命令终端中,进入如下目录:
#进入目录
cd /usr/local/bin/
#创建文件,前者为脚本文件,ups.log为日志保存文件。
touch ups-safe-power.sh ups.log
#赋予文件权限
chmod -R 777 ups-safe-power.sh
chmod -R 777 ups.log
step2: 编辑ups-safe-power.sh脚本,可以使用PVE系统默认的编辑器vi,或者本地局域网电脑上使用VS code编辑器编辑,在文件中添加如下内容:
#!/bin/bash
# 家里局域网内摄像头、路由器IP
target_ip=192.168.0.166
# 累计ping失败次数
failure_count=1
# 最大失败次数
shutdown_failure_count_threshold=4
# 保证一旦失败一次后,时间只输出一次到日志
const=1
# 命令行测试显示
echo "The script is running successfully!"
while :
do
while
ping -c 1 $target_ip > /dev/null
[ $? -eq 0 ];
do
sleep 120
done
if [ $failure_count -eq $const ]; then
echo -e " \n `date`:" >> /usr/local/bin/ups.log
fi
echo "---------AC Power Maybe Off, Try To Check Within 30 Seconds ! Attemp $failure_count..." >> /usr/local/bin/ups.log
sleep 20
ping -c 1 $target_ip &> /dev/null
if [ $? -eq 0 ]; then
((failure_count=1))
echo "---------AC Power May Have Been Restore!" >> /usr/local/bin/ups.log
else
((failure_count++))
fi
sleep 10
if [ $failure_count -eq $shutdown_failure_count_threshold ]; then
echo "---------AC Power Has Been Off, Router Is Powered Down! " >> /usr/local/bin/ups.log
/sbin/shutdown -hP now
break
fi
done
exit 0
step3:在命令行终端使用命令进行测试,关闭终端后脚本不再运行。
#进入对应的脚本目录
cd /usr/local/bin
#执行脚本
./ups-safe-power.sh
执行脚本成功后终端会成阻塞状态,并返回:The script is running successfully!
开机自启
step1: 设置service服务文件,在/etc/systemd/system文件夹下创建ups.service
#命令行命令
touch ups.service
step2: 可以使用vi编辑或者VS code编辑文件,在ups.service文件中添加如下代码,并保存:
[Unit]
Description=Run a Custom Script at Startup
After=default.target
[Service]
ExecStart=/usr/local/bin/ups-safe-power.sh
[Install]
WantedBy=default.target
step3:使能开机自启服务,在终端执行命令:
#使能服务
systemctl enable ups.service
#启动服务
service ups start
#查看服务
service ups status
对应成功开启服务后,查看服务返回状态如图:
改进步骤:
step1: 先关闭原PVE中的自动关机脚本
systemctl stop ups.service
step2: 修改PVE中ups-safe-power.sh脚本,更改target_ip为openwrt管理后台地址,执行重启。
sudo vim /usr/local/bin/ups-safe-power.sh
systemctl restart ups.service
step3: 按照PVE系统设置思路,在openwrt中,/usr/local/bin/路径下,创建ups-safe-power.sh和ups.log文件,并赋予权限。
cd /usr/local/bin
sudo vim ups-safe-power.sh
#复制PVE中ups-safe-power脚本中内容到这里,修改target_ip为家里二级路由器地址
sudo chmod 777 ups-safe-power.sh
step4: openwrt开机自启服务和PVE不同,可以使用/etc/init.d/xxxxx 执行。先在/etc/init.d/路径下
cd /etc/init.d/
touch ups-service
chmod +x ups-service
vim ups-service
添加如下内容:
#!/bin/sh /etc/rc.common
START=99
start() {
/usr/local/bin/ups-safe-power.sh
}
step5: 开启开机自启
#开机自启命令
/etc/init.d/ups-service enable
/etc/init.d/application start #启动脚本
/etc/init.d/application stop #停止服务
/etc/init.d/application restart #重启服务
/etc/init.d/application reload #重新加载配置文件,如果失败,重启服务
/etc/init.d/application enable #开启开机自启动
/etc/init.d/application disable #关闭开机自启动
#查询所有服务状态命令
for F in /etc/init.d/* ; do $F enabled && echo $F on || echo $F **disabled**; done
#找到程序的进程号
ps -aux | grep example.py
#杀死该进程
kill -9 进程号
#程序会被杀死,等待个几秒钟,在查看一下
ps -aux | grep example.py
#可以看到程序已经重启