实现非管理型UPS在linux主机上的停电自动关机

时间:2019-09-22
本文章向大家介绍实现非管理型UPS在linux主机上的停电自动关机,主要包括实现非管理型UPS在linux主机上的停电自动关机使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

买了个山特的SANTAK TG-BOX 850 UPS,自带USB通讯线缆。本以为官方软件提供Linux下的CLI命令以监控UPS状态.

官网提供的下载链接巨慢无比不说,CLI下只提供了安装脚本,没有状态监控程序。我TM……

算了,当个非管理型的UPS用吧。搜索了下网上提供的脚本,感觉写的都不太合适。自己重新修改了一下。

#!/bin/sh
while true
do
    ping -c 1 192.168.50.2 > /dev/null
    ret=$?
    if [ $ret -ne 0 ]
    then
    echo -e "AC POWER LOSS! UPS WORKING NOW!\n-------------------------------\nSave your jobs and a further check will be execute in 3 mins,if it still loss,system will be shutdown immediately!" | wall
    sleep 180
    ping -c 1 192.168.50.2 > /dev/null
    ret=$?
        if [ $ret -ne 0 ]
        then
        echo -e "AC POWER STILL LOSS! SYSTEM WILL SHUTDOWN NOW" | wall
        poweroff
        fi
    fi
done

然后把ups.sh的功能注册成服务,方便操作:

新建/usr/lib/systemd/system/ups.service

[Unit]
Description=UPS monitor and shutdown automaticlly
After=network.target
 
[Service]
Type=simple
User=root
ExecStart=nohup /root/ups.sh &
ExecStop=/bin/kill $MAINPID
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

启动服务:

systemctl start ups.service

让服务开机自动运行

systemctl enable ups.service

done.

原文地址:https://www.cnblogs.com/mrcoolfuyu/p/11568370.html