Bash Shell脚本旋转文件,同时文件继续使用数据更新

Bash Shell脚本旋转文件,同时文件继续使用数据更新,bash,logging,Bash,Logging,我需要一个shell脚本来旋转日志文件,同时不断更新日志文件 #!/bin/bash log-file=$1 if [ ! -f $log-file ]; then echo "log file not found $log-file" exit 1 fi times-tamp=`date +%Y%m%d` new-log-file=$log-file.$time-stamp cp $log-file $new-log-file cat /dev/null > $log-file

我需要一个shell脚本来旋转日志文件,同时不断更新日志文件

#!/bin/bash
log-file=$1
if [ ! -f $log-file ]; then
  echo "log file not found $log-file"
  exit 1
fi
times-tamp=`date +%Y%m%d`
new-log-file=$log-file.$time-stamp
cp $log-file $new-log-file
cat /dev/null > $log-file
gzip -f -9 $new-log-file
我想知道在将消息从日志文件复制到新日志文件期间,飞行中消息的状态

这些信息会丢失吗?如果我们丢失了飞行中的信息,那么我该如何旋转这些文件


有人能帮我理解吗?

这取决于写入日志的过程。 最好的方法是将日志文件移动到新位置,创建新文件,并使用日志向进程发送一些信号,迫使其开始使用新文件 比如:

但您最好使用
Apache logrotate示例:

/var/log/apache2/*.log {
    weekly
    missingok
    rotate 52
    compress
    delaycompress
    notifempty
    create 640 root adm
    sharedscripts
    postrotate
            /etc/init.d/apache2 reload > /dev/null
    endscript
    prerotate
            if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
                    run-parts /etc/logrotate.d/httpd-prerotate; \
            fi; \
    endscript
    }

有一些实用程序提供对消息日志的支持。
除了建议的
logrotate
,我通常使用
syslogd
在文件达到所需的最大尺寸时旋转文件。

不确定这是否是您需要的,但我的方法是创建每日文件,并使用
find./-name log*-mtime+5-删除可能重复的文件
/var/log/apache2/*.log {
    weekly
    missingok
    rotate 52
    compress
    delaycompress
    notifempty
    create 640 root adm
    sharedscripts
    postrotate
            /etc/init.d/apache2 reload > /dev/null
    endscript
    prerotate
            if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
                    run-parts /etc/logrotate.d/httpd-prerotate; \
            fi; \
    endscript
    }