使用 while 循环和 bash 重定向将 s3 cp 日志重定向到单个文件中不起作用 |正在创建多个日志文件

Redirecting s3 cp logs into a single file using while loop and bash redirection is not working | multiple log files are getting created

我正在尝试使用 bash shell 脚本将 s3 cp 命令输出保存到单个日志文件。我的脚本从另一个名为 output.tsv 的文件中获取源和目标存储桶信息,该文件实际上是 while 循环的输入。像下面。我正在使用 &>> 将输出重定向到时间戳为

的文件
while read SOURCE DESTINATION
do
    aws s3 cp $SOURCE $DESTINATION &>> /shared_data/logs/logs-$(date +"%b-%d-%Y-%H-%M-%Z").log
done < output.tsv

s3 cp 日志应该 redirect/append 到单个日志文件,但我看到日志文件每分钟创建一次,而不是将其重定向到单个文件。仅供参考,我正在尝试复制大量文件,我的脚本运行了一个小时。

-rwxrwxr-- 1 user1 group1 33K Oct 28 06:13 logs-Oct-28-2021-06-12-PDT.log

-rwxrwxr-- 1 user1 group1 33K Oct 28 06:14 logs-Oct-28-2021-06-13-PDT.log

-rwxrwxr-- 1 user1 group1 53K Oct 28 06:15 logs-Oct-28-2021-06-14-PDT.log

(我不是 AWS S3 用户但是)关于您的用例仅与 shell 问题相关,您可以在 while 循环之外定义文件名:

log="/shared_data/logs/logs-$(date +"%b-%d-%Y-%H-%M-%Z").log"
while read SOURCE DESTINATION; do
    aws s3 cp "$SOURCE" "$DESTINATION" &>> "$log"
done < output.tsv

顺便说一句,请注意,您可能想要更改 date 格式字符串以使用更标准(ISO-8601 之类)编码:

log="/shared_data/logs/logs-$(date -u +"%Y-%m-%d_%H-%M-%S").log"

这个 date 命令的优点是:

  • 事实上,按字典顺序,字符串排序顺序与日期的排序顺序相匹配;
  • 并使用 UTC 时区(感谢 option -u):
$ date -u +"%Y-%m-%d_%H-%M-%S"
2021-10-28_16-42-00