Achieve unmanaged UPS power failure on linux host of automatic shutdown

Bought Hills of SANTAK TG-BOX 850 UPS, comes with USB communications cable. I thought the official software under Linux CLI command to monitor the UPS status.

Download Link giant slow official website provides unparalleled did not say, the CLI provides only installation script, no state monitoring program. I TM ......

Well, when the non-management type of UPS with it. Search a script available online, the feeling of writing are not appropriate. Own re-edit a bit.

#!/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

Then ups.sh registered as a service function, easy to operate:

New /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

Start the service:

systemctl start ups.service

Let the service to start automatically

systemctl enable ups.service

 

done.

Guess you like

Origin www.cnblogs.com/mrcoolfuyu/p/11568370.html