我需要编写一个用于CentOS工作的脚本,如果文件夹中包含文件名中包含特定字符串的文件(如customer1和customer2),则定期(可能每3小时)检查一次,如果不是,则应该发送邮件给某个地址。
我已经找到了如何在文件名中找到带有特定字符串的文件:
#!/bin/bash
source="/FILES/user/directory1"
string="customer1"
for file in "$source"/*; do
if [[ $file =~ $string ]]; then
echo "do something"
fi
done

如何定期检查以及如何查找多个字符串?
谢谢
答案 0 :(得分:1)
首先,您的服务器必须能够使用任何MTA(邮件传输代理)发送邮件,我的意思是连接到SMTP服务器并配置了MTA文件......
我会说你的服务器已经配置了postfix并且它正常工作,我宁愿使用find
命令来编写你的脚本:
#!/bin/bash
Source="/FILES/user/directory1"
string="customer1"
mail="yourmail@maildomain.com"
Count=$(find $Source -type f -name "$string" | wc -l)
if [ "$Count" -gt 0 ]
then
echo "Number of files matching $string is $Count"
else
echo "Sending mail to $mail, files don't match with customer string"
echo "Filenames don't match with $string" | sendmail -t $mail
fi
如果您希望每隔3小时进行一次此检查,我建议您在新文件/etc/cron.d/checkFiles
# m h dom mon dow user command
0 */3 * * * root YourScriptLocation.sh >> /var/log/checkFiles.log 2>&1
我已经为root用户配置了cronjob,尽管您可以使用任何有权执行脚本的用户配置作业,检查$Source
目录和sendmail中的文件。
答案 1 :(得分:0)
我只是将缺少的要求添加到Alvaro Nino的答案中。请在代码中查看我的评论,以了解此脚本的工作原理。
a2dismod php7.0
systemctl restart apache2