sh/Limiting_Shut_down.sh

34 lines
909 B
Bash
Raw Normal View History

2024-04-28 15:22:05 +08:00
#!/bin/bash
# 获取总的接收和发送流量
output=$(awk 'BEGIN { rx_total = 0; tx_total = 0 }
2025-02-20 14:51:02 +08:00
$1 ~ /^(eth|ens|enp|eno)[0-9]+/ { rx_total += $2; tx_total += $10 }
2024-04-28 15:22:05 +08:00
END {
2024-04-29 09:54:28 +08:00
printf("%.0f Bytes %.0f Bytes", rx_total, tx_total);
2024-04-28 15:22:05 +08:00
}' /proc/net/dev)
2025-02-20 14:51:02 +08:00
2024-04-29 09:54:28 +08:00
# 获取接收和发送的流量数据
2024-04-28 15:22:05 +08:00
rx=$(echo "$output" | awk '{print $1}')
tx=$(echo "$output" | awk '{print $3}')
# 显示当前流量使用情况
2024-04-29 09:54:28 +08:00
echo "当前接收流量: $rx"
echo "当前发送流量: $tx"
2024-04-28 15:22:05 +08:00
threshold_gb=110
# 将GB转换为字节
threshold=$((threshold_gb * 1024 * 1024 * 1024))
# 检查是否达到流量阈值
2024-04-29 09:54:28 +08:00
if (( $rx > $threshold || $tx > $threshold )); then
echo "流量达到${threshold},正在关闭服务器..."
2024-04-28 15:22:05 +08:00
# 在此处执行关闭服务器的命令,例如:
2024-04-28 15:22:30 +08:00
shutdown -h now
2024-04-28 15:22:05 +08:00
# 或者
# systemctl poweroff
else
2024-04-29 09:54:28 +08:00
echo "当前流量未达到${threshold},继续监视..."
2024-04-28 15:22:05 +08:00
fi