nicholasldf 发表于 2020-3-4 17:58

linux shell脚本:检测UPS电源掉电时,服务器自动关机

作为嵌入式工程师,兼职干了公司网管一职,
之前公司服务器没有UPS,断电几次就无法启动了,硬盘拿去修复花了好多K money。
配了个山特UPS,用shell实现检测市电断电保护。

#!/bin/bash
### BEGIN INIT INFO
# Provides:         
# Required-Start:    $local_fs $network
# Required-Stop:   $local_fs
# Default-Start:   2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: auto power off shell
# Description:       auto power off
### END INIT INFO

#Wrote by nich 2020-01-04

#echo "--- ups check shell ---"

PingCount=0
loop=30

while [ $loop -gt 0 ]
do
    ping -c 1 -w 1 192.168.11.1 > /dev/null
   
    if [ $? -eq 0 ]
    then
#      echo "Ping OK: $PingCount" > /var/log/syslog
      PingCount=0
    else
      PingCount=$[ $PingCount + 1 ]
      echo "Ping GateWay 192.168.11.1 Failt: $PingCount" > /var/log/syslog
    fi
   
    sleep 1s
   
    loop=$[ $loop - 1 ]
   
    if [ $PingCount -ge 28 ]
    then
      echo "Server Power Off Now"> /var/log/syslog
      shutdown -h now
    fi

done

#echo "---Ups Manage Shell Exit---"> /var/log/syslog

exit 0

啊h哈123 发表于 2022-7-22 17:09

666666
页: [1]
查看完整版本: linux shell脚本:检测UPS电源掉电时,服务器自动关机