1

我需要清除日志文件的内容。然后我尝试使用“cat /dev/null > logfile”。事实上,它有效!但是有一种我无法理解的奇怪行为。清除文件后立即显示为0字节的大小,但经过一次修改后,大小又恢复到以前的值。使用“du”我可以看到这个值是错误的。我做对了吗?我该如何纠正?

我的猫命令:

jorplov@sg0080b:/applications/fsc/base/logs> ls -lah
-rw-r--r-- 1 jorplov svcusr  10G 2013-11-15 05:18 sg0080b_jorplov_startup.log


jorplov@sg0080b:/applications/fsc/base/logs> df -h .
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg00-fsc  2.0G  1.8G   90M  96% /applications/fsc


jorplov@sg0080b:/applications/fsc/base/logs> cat /dev/null > sg0080b_jorplov_startup.log


jorplov@sg0080b:/applications/fsc/base/logs> df -h .
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg00-fsc  2.0G  365M  1.6G  20% /applications/fsc


jorplov@sg0080b:/applications/fsc/base/logs> ls -lah
total 20K
-rw-r--r-- 1 jorplov svcusr    0 2013-11-15 05:25 sg0080b_jorplov_startup.log

几秒钟后:

jorplov@sg0080b:/applications/fsc/base/logs> ls -lah
-rw-r--r-- 1 jorplov svcusr  10G 2013-11-15 05:26 sg0080b_jorplov_startup.log


jorplov@sg0080b:/applications/fsc/base/logs> stat sg0080b_jorplov_startup.log
  File: `sg0080b_jorplov_startup.log'
  Size: 10718153084     Blocks: 32         IO Block: 4096   regular file
Device: fd03h/64771d    Inode: 82380       Links: 1
Access: (0644/-rw-r--r--)  Uid: (30013/ jorplov)   Gid: (21459/  svcusr)
Access: 2013-11-15 05:34:00.000000000 +0100
Modify: 2013-11-15 05:34:12.000000000 +0100
Change: 2013-11-15 05:34:12.000000000 +0100

第二次尝试:

jorplov@sg0080b:/applications/fsc/base/logs> > sg0080b_jorplov_startup.log


jorplov@sg0080b:/applications/fsc/base/logs> stat sg0080b_jorplov_startup.log
  File: `sg0080b_jorplov_startup.log'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd03h/64771d    Inode: 82380       Links: 1
Access: (0644/-rw-r--r--)  Uid: (30013/ jorplov)   Gid: (21459/  svcusr)
Access: 2013-11-15 05:34:00.000000000 +0100
Modify: 2013-11-15 05:46:55.000000000 +0100
Change: 2013-11-15 05:46:55.000000000 +0100


jorplov@sg0080b:/applications/fsc/base/logs> ls -lah
-rw-r--r-- 1 jorplov svcusr    0 2013-11-15 05:46 sg0080b_jorplov_startup.log

再次,几秒钟后:

jorplov@sg0080b:/applications/fsc/base/logs> stat sg0080b_jorplov_startup.log
  File: `sg0080b_jorplov_startup.log'
  Size: 10718153546     Blocks: 32         IO Block: 4096   regular file
Device: fd03h/64771d    Inode: 82380       Links: 1
Access: (0644/-rw-r--r--)  Uid: (30013/ jorplov)   Gid: (21459/  svcusr)
Access: 2013-11-15 05:34:00.000000000 +0100
Modify: 2013-11-15 05:53:12.000000000 +0100
Change: 2013-11-15 05:53:12.000000000 +0100


jorplov@sg0080b:/applications/fsc/base/logs> ls -lah
-rw-r--r-- 1 jorplov svcusr  10G 2013-11-15 05:53 sagm061_jorplov_startup.log


jorplov@sg0080b:/applications/fsc/base/logs> du -h sagm061_jorplov_startup.log
16K     sagm061_jorplov_startup.log
4

1 回答 1

3

这是由于将文本写入此日志文件的进程。

如果进程正在写入这样的日志:

command > log.txt

并且您在外部截断日志,然后一旦将下一行添加command到日志中,它将在前一个文件指针位置之后写入它,并从开始到该文件指针位置用空字节填充文件\0。因此,日志文件的大小将与截断日志文件之前的大小相同。

解决方案:

但是,如果日志被写为:

command >> log.txt

该日志将以“附加模式”写入。在这种模式下,在写入下一行之前,它总是将文件指针移动到文件末尾,这将避免这种情况。您可以随时截断日志文件。

于 2013-11-15T06:35:15.847 回答