Shell - 我怎样才能让 Logrotate 等到文件被复制/写入,然后继续



我有一个很大的日志文件,由cron每小时复制到/var/log/。几分钟后,logrote开始压缩并重命名的工作。

我的问题是,复制过程尚未完成。结果是,复制的日志文件和存档已损坏。

所以我想要的是一个 shell 脚本,它查看复制的文件并检查它是否在使用中。

#!/bin/bash
until err_str=$(lsof /var/log/logfile.txt 2>&1 >/dev/null); do
if [ -n "$err_str" ]; then
# lsof printed an error string, file may or may not be open
echo "lsof: $err_str" >&2
# tricky to decide what to do here, you may want to retry a number of times,
# but for this example just break
break
fi
# lsof returned 1 but didn't print an error string, assume the file is open
sleep 1
done

这是行不通的,有什么问题?

你可以尝试使用-

而 err_str=$(lsof/var/log/logfile.txt 2>/dev/null(; do 如果 [ -z "$err_str" ];然后

最新更新