ファイル変更電子メール通知用のLinuxスクリプト(inotifyまたは他のユーティリティではありません)

ファイル変更電子メール通知用のLinuxスクリプト(inotifyまたは他のユーティリティではありません)

私はファイルの内容が変わるたびに電子メールで通知するLinuxスクリプト(inotifyツールや他のユーティリティではない)を探しています。 Googleでサンプルスクリプトを管理しましたが、手動で実行する必要があり、電子メールは実行されません。

[[ -z `find /path/to/file -mmin -60` ]]

if [ $? -eq 0 ]
then
  echo -e " /path/to/file nothing has changed"
else
  mail -s "/path/to/file has been changed" mailid
fi

ベストアンサー1

目的のタスクを実行するスクリプトがある場合は、スクリプトをcronテーブル(またはcron crontab)に追加すると、目的の頻度でタスクが実行されます。だから:

#!/bin/bash
if ! [[ -z $(find /path/to/file -mmin 60) ]]; then
    # The file was changed, so:
    echo -e "The hash is:\n$(md5sum /path/to/file)" | mail -s "/file has changed on $(hostname -s) [email protected]"
else
    # If this is in the crontab, remove this else stanza; cron
    # jobs should not write to standard output, lest that output
    # be sent to the local mailer daemon to drop it into the owner's
    # mailbox.
    echo "No changes to /path/to/file detected."
fi

おすすめ記事