无法清空日志文件

无法清空日志文件

我工作服务器上有一个我编写的脚本,该脚本每天午夜将日志文件复制到一个文件夹,将该文件夹复制到 Amazon S3 并将原始文件清零。

但是,我发现原始文件尚未清零,这影响了磁盘空间。

我使用的命令如下:

cat /dev/null | tee /etc/vinter/logs/*

今天我尝试单独使用这个命令,但没有成功。之后,我又尝试了另外两个命令,但都没有成功。

[root@APPSERVER11-S1 ~]# cat /dev/null | tee /etc/vinter/logs/*
[root@APPSERVER11-S1 ~]# ls -l /etc/vinter/logs/*
-rw-r--r-- 1 root root 9223905280 Sep 11 12:47 /etc/vinter/logs/rsp.appserver11-s1.apigoldnode-1.rsp.log
-rw-r--r-- 1 root root    9468202 Sep 11 12:47 /etc/vinter/logs/Services-APIGOLDNODE-1.log
-rw-r--r-- 1 root root          0 Sep 11 12:47 /etc/vinter/logs/Services-APIGOLDNODE-1.log.1
[root@APPSERVER11-S1 ~]#
[root@APPSERVER11-S1 ~]#
[root@APPSERVER11-S1 ~]# echo -n | tee /etc/vinter/logs/*
[root@APPSERVER11-S1 ~]# ls -l /etc/vinter/logs/*
-rw-r--r-- 1 root root 9224224768 Sep 11 12:48 /etc/vinter/logs/rsp.appserver11-s1.apigoldnode-1.rsp.log
-rw-r--r-- 1 root root    9493203 Sep 11 12:48 /etc/vinter/logs/Services-APIGOLDNODE-1.log
-rw-r--r-- 1 root root          0 Sep 11 12:47 /etc/vinter/logs/Services-APIGOLDNODE-1.log.1
[root@APPSERVER11-S1 ~]#
[root@APPSERVER11-S1 ~]#
[root@APPSERVER11-S1 ~]# printf '' | tee /etc/vinter/logs/*
[root@APPSERVER11-S1 ~]# ls -l /etc/vinter/logs/*
-rw-r--r-- 1 root root 9224380416 Sep 11 12:48 /etc/vinter/logs/rsp.appserver11-s1.apigoldnode-1.rsp.log
-rw-r--r-- 1 root root    9502868 Sep 11 12:48 /etc/vinter/logs/Services-APIGOLDNODE-1.log
-rw-r--r-- 1 root root          0 Sep 11 12:48 /etc/vinter/logs/Services-APIGOLDNODE-1.log.1

单独来看它也无法正常工作:

[root@APPSERVER11-S1 ~]# echo > /etc/vinter/logs/rsp.appserver11-s1.apigoldnode-1.rsp.log
[root@APPSERVER11-S1 ~]# ls -l /etc/vinter/logs/*
-rw-r--r-- 1 root root 9236357120 Sep 11 12:58 /etc/vinter/logs/rsp.appserver11-s1.apigoldnode-1.rsp.log
-rw-r--r-- 1 root root     520807 Sep 11 12:58 /etc/vinter/logs/Services-APIGOLDNODE-1.log
-rw-r--r-- 1 root root   10264642 Sep 11 12:54 /etc/vinter/logs/Services-APIGOLDNODE-1.log.1

根据评论中的说明,该truncate命令没有起到作用:

[root@APPSERVER11-S1 ~]# truncate -s 0 /etc/vinter/logs/rsp.appserver11-s1.apigoldnode-1.rsp.log
[root@APPSERVER11-S1 ~]# ls -l /etc/vinter/logs/
total 8292
-rw-r--r-- 1 root root 9283125248 Sep 11 13:32 rsp.appserver11-s1.apigoldnode-1.rsp.log
-rw-r--r-- 1 root root    6026198 Sep 11 13:32 Services-APIGOLDNODE-1.log
-rw-r--r-- 1 root root          0 Sep 11 13:06 Services-APIGOLDNODE-1.log.1

版本tee为:

[root@APPSERVER11-S1 ~]# tee --version
tee (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Parker, Richard M. Stallman, and David MacKenzie.

文件系统是xfs

[root@APPSERVER11-S1 ~]# df -Th
Filesystem                     Type      Size  Used Avail Use% Mounted on
devtmpfs                       devtmpfs  7.3G     0  7.3G   0% /dev
tmpfs                          tmpfs     7.3G     0  7.3G   0% /dev/shm
tmpfs                          tmpfs     7.3G  185M  7.1G   3% /run
tmpfs                          tmpfs     7.3G     0  7.3G   0% /sys/fs/cgroup
/dev/mapper/vg_main-lv_root    xfs        16G   14G  1.8G  89% /
/dev/mapper/vg_dados-lv_dados  xfs        20G   33M   20G   1% /datastorage
/dev/mapper/vg_dados-lv_docker btrfs      80G  128M   79G   1% /var/lib/docker
/dev/xvdb1                     xfs       497M  161M  337M  33% /boot
tmpfs                          tmpfs     1.5G     0  1.5G   0% /run/user/0
tmpfs                          tmpfs     1.5G     0  1.5G   0% /run/user/1002

我尝试在另一台服务器上执行此操作,只有最频繁更新的文件(每秒有很多行)没有被归零。可能是因为文件更新太频繁,甚至无法被归零?

[root@APPSERVER21-S1 ~]# ls -l /etc/vinter/logs/
total 2046352
-rw-r--r-- 1 root root     3920496 Sep 11 14:35 PlusoftCRMIntegration-APISILVERNODE-1.log
-rw-r--r-- 1 root root 30891423129 Sep 11 15:27 rsp.appserver21-s1.apigoldnode-1.api-oi-gold.log
-rw-r--r-- 1 root root   406151168 Sep 11 15:27 rsp.appserver21-s1.apisilvernode-1.api-oi-silver.log
[root@APPSERVER21-S1 ~]# cat /dev/null | tee /etc/vinter/logs/*
[root@APPSERVER21-S1 ~]# ls -l /etc/vinter/logs/
total 492
-rw-r--r-- 1 root root           0 Sep 11 15:28 PlusoftCRMIntegration-APISILVERNODE-1.log
-rw-r--r-- 1 root root 30892328752 Sep 11 15:28 rsp.appserver21-s1.apigoldnode-1.api-oi-gold.log
-rw-r--r-- 1 root root           0 Sep 11 15:28 rsp.appserver21-s1.apisilvernode-1.api-oi-silver.log

第二台服务器的/分区文件系统xfs与第一台一样。

[root@APPSERVER21-S1 ~]# df -Th
Filesystem                     Type      Size  Used Avail Use% Mounted on
devtmpfs                       devtmpfs  3.6G     0  3.6G   0% /dev
tmpfs                          tmpfs     3.6G     0  3.6G   0% /dev/shm
tmpfs                          tmpfs     3.6G  137M  3.4G   4% /run
tmpfs                          tmpfs     3.6G     0  3.6G   0% /sys/fs/cgroup
/dev/mapper/vg_main-lv_root    xfs        16G  3.0G   13G  20% /
/dev/mapper/vg_dados-lv_dados  xfs        20G   33M   20G   1% /datastorage
/dev/mapper/vg_dados-lv_docker btrfs      80G  128M   79G   1% /var/lib/docker
/dev/xvdb1                     xfs       497M  161M  337M  33% /boot
tmpfs                          tmpfs     722M     0  722M   0% /run/user/0

有时某些日志文件会被清零,但几秒钟后就会恢复到其原始大小。

[root@APPSERVER11-S1 ~]# cat /dev/null | tee /etc/vinter/logs/*
[root@APPSERVER11-S1 ~]# ls -l /etc/vinter/logs/
total 24
-rw-r--r-- 1 root root       0 Sep 11 18:13 rsp.appserver11-s1.apigoldnode-1.rsp.log
-rw-r--r-- 1 root root 3125765 Sep 11 18:13 Services-APIGOLDNODE-1.log
-rw-r--r-- 1 root root       0 Sep 11 18:13 Services-APIGOLDNODE-1.log.1
[root@APPSERVER11-S1 ~]# ls -l /etc/vinter/logs/
total 160
-rw-r--r-- 1 root root 9618808832 Sep 11 18:13 rsp.appserver11-s1.apigoldnode-1.rsp.log
-rw-r--r-- 1 root root    3131397 Sep 11 18:13 Services-APIGOLDNODE-1.log
-rw-r--r-- 1 root root          0 Sep 11 18:13 Services-APIGOLDNODE-1.log.1

可能出现什么问题?

答案1

您是否尝试过通过运行强制删除整个文件?

# rm /var/log/logfile -f

相关内容