bash脚本监控文件当被修改时通知给个实例
以下是一个简单的bash脚本,用于监控指定文件的修改,并在文件被修改时发送通知邮件。
#!/bin/bash
# 监控文件路径
file_path="/path/to/file"
# 上次文件修改时间
last_modified=$(stat -c %Y $file_path)
# 发送邮件的收件人
recipient="youremail@example.com"
# 循环监控文件
while true
do
# 获取当前文件修改时间
current_modified=$(stat -c %Y $file_path)
# 判断文件是否被修改
if [ $current_modified -ne $last_modified ]
then
# 发送邮件通知文件被修改
echo "The file $file_path has been modified." | mail -s "File Modification Alert" $recipient
# 更新上次文件修改时间
last_modified=$current_modified
fi
# 等待5秒钟后继续监控文件
sleep 5
done
在使用此脚本时,需要将/path/to/file替换为实际的文件路径,并将youremail@example.com替换为实际的收件人邮箱地址。脚本会一直循环监控文件,当文件被修改时会发送邮件通知
原文地址: https://www.cveoy.top/t/topic/epmo 著作权归作者所有。请勿转载和采集!