メール Bash スクリプト

メール Bash スクリプト

私のスクリプトに問題があります

特定の時間に電子メールを送信し、ファイルが100kbを超える場合は説明を含む通知です。

これは私のスクリプトです。私に通知を送信するように設定するにはどうすればよいですか?

#!/bin/bash

file="afile"
maxsize=100

while true; do

        actualsize=$(du -k "$file" | cut -f1)

        if [ $actualsize -ge $maxsize ]
        then
                echo size is over $maxsize kilobytes
                subject="size exceed on file $file"
                emailAddr="[email protected]"
                emailCmd="mail -s \"$exceedfile\" \"$emvpacifico\""
        (echo ""; echo "date: $(date)";) | eval mail -s "$exceedfile" \"[email protected]\"
                exit
        else
                echo size is under $maxsize kilobytes
        fi

        sleep 60
done

ベストアンサー1

インラインコメントを使用してすばやく書き換えます。

#!/bin/bash

file="afile"
maxsize=100

while true; do
   # Use `stat`, the tool for getting file metadata, rather than `du`
   # and chewing on its output.  It gives size in bytes, so divide by
   # 1024 for kilobytes.
   actualsize=$(($(stat --printf="%s" "$file")/1024))
      if [[ $actualsize -ge $maxsize ]]; then
         echo "size is over $maxsize kilobytes"
         subject="size exceed on file $file"
         emailAddr="[email protected]"
         # In almost all cases, if you are using `eval`, either
         # something has gone very wrong, or you are doing a thing in
         # a _very_ suboptimal way.
         echo "Date: $(date)" | mail -s "$subject" "$emailAddr"
      else
         echo "size is under $maxsize kilobytes"
      fi
   sleep 60
done

また、スクリプトを無限ループで実行するのではなく、一度だけ実行するようにスクリプトを変更し、cronテーブルエントリを使用して実行するようにスケジュールすることをお勧めします。

おすすめ記事