打印

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

[复制链接]
1218|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
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

粉丝