【发布时间】:2015-10-25 08:37:03
【问题描述】:
我已在以前版本的 OSX 上成功使用以下脚本,以在磁盘容量达到定义的阈值时提醒我。
在 Yosemite 上运行时,它不起作用:
% bash disk-full-alert.sh
disk: 10766513
df: 10766513: No such file or directory
output:
current:
threshold: 65
_________________
disk-full-alert.sh: line 26: [: -gt: unary operator expected
disk: 129154082
df: 129154082: No such file or directory
output:
current:
threshold: 65
_________________
disk-full-alert.sh: line 26: [: -gt: unary operator expected
disk: 710743471
df: 710743471: No such file or directory
output:
current:
threshold: 65
_________________
disk-full-alert.sh: line 26: [: -gt: unary operator expected
脚本:
#!/bin/bash
PATH=/bin:/usr/bin:/sbin:/usr/sbin export PATH
HOSTNAME='server.example.com'
ERECIP='dan@example.com'
# Threshold is percentage full
THRESHOLD=65
get_data() {
df -l | grep -v Mounted| awk ' { print $6 } '
}
get_data | while IFS= read -r disk
do
echo disk: $disk
OUTPUT=($(LC_ALL=C df -P ${disk}))
echo output: $OUTPUT
CURRENT=$(echo ${OUTPUT[11]} | sed 's/%//')
echo current: $CURRENT
echo threshold: $THRESHOLD
echo _________________
echo " "
[ $CURRENT -gt $THRESHOLD ] &&
(
echo "From: Server Admin <admin@$HOSTNAME>"
echo "To: $ERECIP"
echo "Subject: Warning!! $disk file system is $CURRENT% full on $HOSTNAME"
echo ""
date
echo ""
echo "ALERT!!"
echo " "
echo The $disk file system is $CURRENT% full
echo
) | /usr/sbin/sendmail -it
最受赞赏的想法或建议!
丹
【问题讨论】:
-
请关注MCVE。您已经有了一个良好的开端,但“不工作”不符合错误描述的条件。
标签: macos bash shell osx-yosemite