我对bash脚本很陌生,我正在尝试编写一个bash脚本,它将监视特定数量的文件在一个时间框架内被发送的路径。假设有20个文件预计来自9 am -11 am。
脚本必须能够执行以下操作:
我正在考虑把这个设置在一个索引标签上。只有当文件计数完成或超过11:10 am (有10 分钟宽限期)时,脚本才会停止。
我需要建议如何实现前两个目标。我只能报道没有问题的案子。我有想法,但不知道怎么把它写在剧本里。
到目前为止,这就是我所拥有的:
第1版:
#!/bin/bash
hour=(9 10 11)
server=myserver@domain.com
notify=notify@domain.com
if [[ "${hour[@]}" =~ "$(date +"%H")" ]]; then
if [[ $(ls /fake/path | wc -l) == 20 ]]; then
echo -e "All files received" | mailx -s "All files received" -r $server $notify
fi
fi(由于不建议解析ls输出,我更新了下面的脚本):Version 2:
#!/bin/bash
hour=(9 10 11)
server=myserver@domain.com
notify=notify@domain.com
if [[ "${hour[@]}" =~ "$(date +"%H")" ]]; then
echo $(date +"%Y-%m-%d %H:%M:%S") $1 >> /tmp/record.txt
if [[ $(cat /tmp/record.txt | wc -l) == 20 ]]; then
echo -e "All files received" | mailx -a /tmp/record.txt -s "All files received" -r $server $notify
fi
fi任何想法都欢迎。我可能看错了。
发布于 2020-10-31 16:45:35
您可以使用一个简单的cron作业来完成它。假设您希望在09:10上检查,如果文件计数为零,则发送邮件通知,如果文件计数小于20,则在11:10发送邮件通知。
#!/bin/bash
n="$1"
message="$2"
target_dir="/full/path/to/directory"
cnt=$(find "$target_dir" -type f -printf '\n' | wc -l)
if (( cnt < n )); then
else
fi正如您所看到的,测试文件计数和消息的值是参数。所以你可以
crontab -e添加这些行
10 9 * * * bash /full/path/to/the/scipt.sh 1 "No files yet"
10 11 * * * bash /full/path/to/the/scipt.sh 20 "Files less than 20"保存文件。现在安装了新的crontab。
备注:
find命令的D13参数。我们只为每个文件打印一个新行,并计算换行符,因此任何文件名都会被处理。https://unix.stackexchange.com/questions/617211
复制相似问题