Bash 脚本日志文件轮转

发布于 2024-09-19 03:01:17 字数 412 浏览 6 评论 0原文

我的 bash 脚本生成一个日志文件。现在我想实现一些日志文件轮换。
假设第一次它被称为somelog.log,下次它被重命名为somelog.log.1 > 和新的日志文件somelog.log
第三次新日志又是somelog.log,但是somelog.log.1 重命名为 somelog.log.2,旧的 somelog.log 重命名为 somelog.log.1
我会最多可以授予 5 个。

这是之前完成的吗(示例脚本),有什么建议吗?我很感激任何建议。

My bash script produces a log file. Now i'd like to implement some log file rotation.
Let's say the first time it's called somelog.log, the next time it's renamed to somelog.log.1 and the new log file somelog.log.
The third time the new log is somelog.log again, but somelog.log.1 is renamed to somelog.log.2 and the old somelog.log to somelog.log.1.
I would be able to grant a maximum of eg 5.

Is this done before (sample script), any suggestions. I appreciate any advice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七七 2024-09-26 03:01:17

尝试这个 bash 函数,它需要两个参数:

  1. 文件应超过的最大兆字节数以进行旋转(否则保持不变)
  2. 文件名的完整路径。

来源:

function rotate () {
  # minimum file size to rotate in MBi:
  local MB="$1"
  # filename to rotate (full path)
  local F="$2"
  local msize="$((1024*1024*${MB}))"
  test -e "$F" || return 2

  local D="$(dirname "$F")"
  local E=${F##*.}
  local B="$(basename "$F" ."$E")"

  local s=

  echo "rotate msize=$msize file=$F -> $D | $B | $E"
  if [ "$(stat --printf %s "$F")" -ge $msize ] ; then
     for i in 8 9 7 6 5 4 3 2 1 0; do 
       s="$D/$B-$i.$E"
       test -e "$s" && mv $s "$D/$B-$((i+1)).$E"
  # emtpy command is need to avoid exit iteration if test fails:
       :;
     done &&
     mv $F $D/$B-0.$E
  else
     echo "rotate skip: $F < $msize, skip"
  fi
  return $?
}

Try this bash function, it takes two parameters:

  1. number of maximum megabyte the file should exceed to be rotated (otherwise is let untouched)
  2. full path of the filename.

source:

function rotate () {
  # minimum file size to rotate in MBi:
  local MB="$1"
  # filename to rotate (full path)
  local F="$2"
  local msize="$((1024*1024*${MB}))"
  test -e "$F" || return 2

  local D="$(dirname "$F")"
  local E=${F##*.}
  local B="$(basename "$F" ."$E")"

  local s=

  echo "rotate msize=$msize file=$F -> $D | $B | $E"
  if [ "$(stat --printf %s "$F")" -ge $msize ] ; then
     for i in 8 9 7 6 5 4 3 2 1 0; do 
       s="$D/$B-$i.$E"
       test -e "$s" && mv $s "$D/$B-$((i+1)).$E"
  # emtpy command is need to avoid exit iteration if test fails:
       :;
     done &&
     mv $F $D/$B-0.$E
  else
     echo "rotate skip: $F < $msize, skip"
  fi
  return $?
}
陪我终i 2024-09-26 03:01:17

我刚刚为此编写了一个 bash 脚本:
https://github.com/lingtalfi/logrotator

它基本上检查日志文件的大小,如果超过任意阈值,它将日志文件复制到日志目录中。

它对 cron 友好,或者您也可以手动使用它。

典型的命令如下所示:

> ./logrotator.sh -f private/log -m {fileName}.{datetime}.txt -v

I've just made a bash script for that:
https://github.com/lingtalfi/logrotator

It basically checks your log file's size, and if it exceeds an arbitrary threshold, it copies the log file into a log directory.

It's cron friendly, or you can use it manually too.

A typical command looks like that:

> ./logrotator.sh -f private/log -m {fileName}.{datetime}.txt -v
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文