linux 访问时间 不变化,为什么 linux 的文件访问时间 atime 不会变化?

linux 中使用 stat 命令可以查看文件的属性信息,其中有三个属性:Access、Modify、Change。

Access Time:访问时间,访问文件时会更新这个时间

Modify Time:修改文件内容时间,当文件内容出现变化时会更新这个时间

Change Time:修改文件属性时间,当文件属性发生变化时会更新这个时间

步骤一:

查看文件属性

root@PQ5MBGOMBCJ52TI:~# stat test

File: test

Size: 0 Blocks: 0 IO Block: 4096 regular empty file

Device: 810h/2064d Inode: 43049 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)

Access: 2021-04-24 23:49:26.930000000 +0800

Modify: 2021-04-24 23:49:26.930000000 +0800

Change: 2021-04-24 23:49:26.930000000 +0800

Birth: -

步骤二:

修改文件内容,可以看到修改时间都发生了变化

root@PQ5MBGOMBCJ52TI:~# stat test

File: test

Size: 4 Blocks: 8 IO Block: 4096 regular file

Device: 810h/2064d Inode: 43049 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)

Access: 2021-04-24 23:49:26.930000000 +0800

Modify: 2021-04-24 23:50:04.580000000 +0800

Change: 2021-04-24 23:50:04.580000000 +0800

Birth: -

因为改变了文件内容,所以 Modify Time 变化,而 Modify Time 变化属于文件属性变化,于是也导致了 Change Time 变化,而因为没有读取访问文件,所以 Access Time 没有变化。

步骤三:

接着使用 cat 命令输出文件内容,可以看到 Access Time 也变成最新的了

root@PQ5MBGOMBCJ52TI:~# cat test

123

root@PQ5MBGOMBCJ52TI:~# stat test

File: test

Size: 4 Blocks: 8 IO Block: 4096 regular file

Device: 810h/2064d Inode: 43049 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)

Access: 2021-04-24 23:50:38.010000000 +0800

Modify: 2021-04-24 23:50:04.580000000 +0800

Change: 2021-04-24 23:50:04.580000000 +0800

Birth: -

事情到这里时还是符合预料的,然而也就从这里开始出现问题了,当再次重复使用 cat 命令访问文件时,照理来说 Access Time 应该会再次更新,但事实并不是。

步骤四:

root@PQ5MBGOMBCJ52TI:~# date

Sat Apr 24 23:53:49 CST 2021

root@PQ5MBGOMBCJ52TI:~#

root@PQ5MBGOMBCJ52TI:~# cat test

123

root@PQ5MBGOMBCJ52TI:~# stat test

File: test

Size: 4 Blocks: 8 IO Block: 4096 regular file

Device: 810h/2064d Inode: 43049 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)

Access: 2021-04-24 23:50:38.010000000 +0800

Modify: 2021-04-24 23:50:04.580000000 +0800

Change: 2021-04-24 23:50:04.580000000 +0800

Birth: -

可以看到访问时间依然是之前那个,并没有变化。

为什么?WHY?

为了找到答案,期间各种搜索不提,最终在man mount中找到这样的一个说明:

relatime

Update inode access times relative to modify or change time. Access time is only updated if the previous access time was earlier than the current modify or change time. (Similar to noatime, but it doesn't break mutt

or other applications that need to know if a file has been read since the last time it was modified.)

Since Linux 2.6.30, the kernel defaults to the behavior provided by this option (unless noatime was specified), and the strictatime option is required to obtain traditional semantics. In addition, since Linux 2.6.30,

the file's last access time is always updated if it is more than 1 day old.

上面大概意思是:当文件系统使用 relatime 时,Access Time 就取决于 Modify Time 或 Change Time,当访问文件时,如果此时 Access Time 是小于 Modify Time 或 Change Time,那么就会更新 Access Time,否则就不会更新。在 linux 内核版本 2.6.30 之后都会默认使用这种方式更新访问时间,另外访问时间超过了一天也会导致它更新。这里还提到了 noatime,也可以查下它的说明:

noatime

Do not update inode access times on this filesystem (e.g. for faster access on the news spool to speed up news servers). This works for all inode types (directories too), so it implies nodiratime.

意思是说文件系统使用了 noatime,那么将不会更新文件的访问时间了。

至于为什么会有这个访问时间的限制,是因为 linux 的访问时间本来就是令人诟病的一种设计,本来就只是访问文件,如果每次都更新访问时间,那么每次读取都会导致磁盘写入,效率很低,所以才有 relatime 这样设计。不用 noatime 配置是因为不更新访问时间就会导致一些文件备份恢复出现问题。总的来说就是一个历史遗留的缺陷。

最后可以使用mount命令查看文件系统使用了哪种更新访问时间的行为,现在一般都是用 relatime 了:

root@PQ5MBGOMBCJ52TI:~# mount

/dev/sdb on / type ext4 (rw,relatime,discard,errors=remount-ro,data=ordered)

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值