
ディスク容量が90を超えると電子メールを記録するためにこのスクリプトを作成しました。出力を別々の行で取得するのに役立ちます。これは私のコードです。
#!/bin/bash
errortext=""
EMAILS="[email protected]"
for line in `df | awk '{print$6, $5, $4, $1} ' `
do
# get the percent and chop off the %
percent=`echo "$line" | awk -F - '{print$5}' | cut -d % -f 1`
partition=`echo "$line" | awk -F - '{print$1}' | cut -d % -f 1`
# Let's set the limit to 90% when alert should be sent
limit=90
if [[ $percent -ge $limit ]]; then
errortext="$errortext $line"
fi
done
# send an email
if [ -n "$errortext" ]; then
echo "$errortext" | mail -s "NOTIFICATION: Some partitions on almost
full" $EMAILS
fi
答え1
出力を変数に保存しようとしないでください。必要でないときにコマンド出力を繰り返さないでください。
#!/bin/bash
mailto=( [email protected] [email protected] )
tmpfile=$( mktemp )
df | awk '0+$5 > 90' >"$tmpfile"
if [ -s "$tmpfile" ]; then
mail -s 'NOTIFICATION: Some partitions on almost full' "${mailto[@]}" <"$tmpfile"
fi
rm -f "$tmpfile"
行の割合が90%を超える場合は、関連する出力行を配列にリストされているdf
アドレスにメールで送信します。 5番目のフィールドが数値として解釈されるように強制mailto
します。ファイルが空でない場合、ファイルのテストは成功します。一時ファイルを作成し、その名前を返します。0+$5
awk
-s
mktemp