Bash脚本,用于监视多个路径,并在添加新文件时结束电子邮件通知



我已经编写了一个脚本来监控两个目录,如果在这里添加了新文件,则会结束电子邮件通知,但它似乎只监控第一个目录,而不是第二个目录(当我向第二个目录添加内容时,没有收到通知(,有人能帮我解决这个问题吗?脚本:

#!/bin/bash
monitor_dir=/path1/UnSent
monitor_dir1=/path2/failed
email=email1.com
email2=email2.com

files=$(find "$monitor_dir" -maxdepth 1 | sort)
IFS=$'n'
while true
do
sleep 5s
newfiles=$(find "$monitor_dir" -maxdepth 1 | sort)
added=$(comm -13 <(echo "$files") <(echo "$newfiles"))
[ "$added" != "" ] &&
find $added -maxdepth 1 -printf '%Tct%st%pn' |
mail -s "your file sent to UnSent" "$email" 
files="$newfiles" 
done
files1=$(find "$monitor_dir1" -maxdepth 1 | sort)
IFS=$'n'
while true
do
sleep 5s
newfiles1=$(find "$monitor_dir1" -maxdepth 1 | sort)
added=$(comm -13 <(echo "$files1") <(echo "$newfiles1"))
[ "$added" != "" ] &&
find $added -maxdepth 1 -printf '%Tct%st%pn' |
mail -s "your file sent to failed" "$email2" 
files1="$newfiles1" 
done

在第一个循环done &之后添加一个与号&可能会有所帮助。这将在后台运行第一个循环,然后继续第二个循环。如果没有&,第二个循环将永远无法到达,因为第一个循环无限期运行。

更新后的代码:

#!/bin/bash
monitor_dir=/path1/UnSent
monitor_dir1=/path2/failed
email=email1.com
email2=email2.com

files=$(find "$monitor_dir" -maxdepth 1 | sort)
IFS=$'n'
while true
do
sleep 5s
newfiles=$(find "$monitor_dir" -maxdepth 1 | sort)
added=$(comm -13 <(echo "$files") <(echo "$newfiles"))
[ "$added" != "" ] &&
find $added -maxdepth 1 -printf '%Tct%st%pn' |
mail -s "your file sent to UnSent" "$email" 
files="$newfiles" 
done &
files1=$(find "$monitor_dir1" -maxdepth 1 | sort)
IFS=$'n'
while true
do
sleep 5s
newfiles1=$(find "$monitor_dir1" -maxdepth 1 | sort)
added=$(comm -13 <(echo "$files1") <(echo "$newfiles1"))
[ "$added" != "" ] &&
find $added -maxdepth 1 -printf '%Tct%st%pn' |
mail -s "your file sent to failed" "$email2" 
files1="$newfiles1" 
done

相关内容

  • 没有找到相关文章

最新更新