---
title: "Linux Ping实现停电关机脚本"
id: "375"
type: "post"
slug: "linux-ping%e5%ae%9e%e7%8e%b0%e5%81%9c%e7%94%b5%e5%85%b3%e6%9c%ba%e8%84%9a%e6%9c%ac"
published_at: "2021-07-13T10:50:09+00:00"
modified_at: "2021-07-13T10:50:09+00:00"
url: "https://www.hitsword.org/2021/375.html"
markdown_url: "https://www.hitsword.org/2021/375.md"
excerpt: "路由器IP地址192.168.5.1，没有接UPS。 把下面脚本写到/root/powercheck.sh并c […]"
taxonomy_category:
  - "Debian"
  - "运维笔记"
taxonomy_post_tag:
  - "Ping"
  - "停电关机"
---

# Linux Ping实现停电关机脚本

路由器IP地址192.168.5.1，没有接UPS。  
 把下面脚本写到/root/powercheck.sh并chmod +x /root/powercheck.sh赋予执行权限。  
 crontab -e 添加 * * * * * /root/powercheck.sh 每分钟执行。

```
#!/bin/sh
ret=`ping -c 3 192.168.5.1 | grep '0 received' | wc -l`
if [ $ret -eq 0 ];
then
  echo 'AC Power OK' > /root/powertest.txt
else
  poweroff
fi
```
