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

[复制链接]
 楼主| nicholasldf 发表于 2020-3-4 17:58 | 显示全部楼层 |阅读模式
作为嵌入式工程师,兼职干了公司网管一职
之前公司服务器没有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
您需要登录后才可以回帖 登录 | 注册

本版积分规则

61

主题

261

帖子

10

粉丝
快速回复 在线客服 返回列表 返回顶部