【问题标题】:Shell - How can I bring Logrotate to wait until file is copied/written and than go onShell - 我怎样才能让 Logrotate 等到文件被复制/写入然后继续
【发布时间】:2019-02-05 05:46:02
【问题描述】:

我有一个很大的日志文件,它由 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 不会总是空无一物吗?您正在将所有内容重定向到 /dev/null。

标签: shell loops logrotate


【解决方案1】:

你可以试试-

while err_str=$(lsof /var/log/logfile.txt 2>/dev/null);做 如果 [ -z "$err_str" ];那么

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 2019-05-21
    • 1970-01-01
    • 2012-02-27
    • 2020-07-22
    • 2019-09-02
    相关资源
    最近更新 更多