cat后atime的问题(已解决)

发现cat命令cat一个系统本来就有的文件,cat 后atime时间没有变。如果cat一个自己创建的文件,atime会改变。

如下:1.txt,使用cat查看后,atime改变;/etc/selinux/config文件使用cat命令后atime不变。纠结了!有知道是什么情况的朋友告诉我下,邮箱:chiugui@qq.com

 当前环境:

[root@backup ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[root@backup ~]# uname -r
3.10.0-957.el7.x86_64
r[root@backup ~]# echo "hello" >1.txt
[root@backup ~]# ll
total 4
-rw-r--r-- 1 root root 6 Apr 13 20:35 1.txt
[root@backup ~]# stat 1.txt 
  File: ‘1.txt’
  Size: 6             Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d    Inode: 33559406    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-04-13 20:35:35.469055828 +0800    #cat前
Modify: 2020-04-13 20:35:35.469055828 +0800
Change: 2020-04-13 20:35:35.469055828 +0800
 Birth: -
[root@backup ~]# cat 1.txt 
hello
[root@backup ~]# stat 1.txt 
  File: ‘1.txt’
  Size: 6             Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d    Inode: 33559406    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-04-13 20:36:11.916457040 +0800    #atime改变
Modify: 2020-04-13 20:35:35.469055828 +0800
Change: 2020-04-13 20:35:35.469055828 +0800
 Birth: -
[root@backup ~]# stat /etc/selinux/config 
  File: ‘/etc/selinux/config’
  Size: 542           Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d    Inode: 101726835   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-04-13 13:22:18.506554335 +0800  #cat前
Modify: 2019-04-19 12:35:11.658922642 +0800
Change: 2019-04-19 12:35:11.658922642 +0800
 Birth: -
[root@backup ~]# cat /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 


[root@backup ~]# stat /etc/selinux/config 
  File: ‘/etc/selinux/config’
  Size: 542           Blocks: 8          IO Block: 4096   regular file
Device: 802h/2050d    Inode: 101726835   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-04-13 13:22:18.506554335 +0800   #cat后
Modify: 2019-04-19 12:35:11.658922642 +0800
Change: 2019-04-19 12:35:11.658922642 +0800
 Birth: -

最后在网上爬文,发现原来是以下原因

以下出于 https://blog.csdn.net/cjf_iceking/java/article/details/11988525


起初我也怀疑过是不是OS的bug导致的,后来发现,在kernel版本2.6.30之前,linux的核心开发人员针对Ext3/Ext4文件系统的性能进行了讨论,其中包括atime。在kernel 2.6.30之前,文件系统中默认会及时的更新atime,这样会带来两个问题:

(1)    系统中大量的文件访问,将atime写入到磁盘中,消耗时间,从而降低性能

(2)    这样的操作也会消耗电能

在Linux上运行的,很少的应用程序需要获取精确的atime时间,并且Linux核心开发人员从Ext3/Ext4文件系统的性能角度出发,决定在2.6.30版本的内核中修改atime的更新方式,只有在以下三种情况之一才会更新atime:

(1)    如果将分区mount的挂载的时候指定采用非relatime方式(默认采用relatime方式),如strictatime.

补充:在OS启动的时候,将各个分区挂载到不同的目录,在挂载(mount)的参数中采用strictatime,表明及时更新atime。在2.6.30之后mount添加了”relatime”和”strictatime”两个选项,详细的可以通过”man mount”查看。

(2) atime小于ctime或者小于mtime的时候

(3) 本次的access time和上次的atime超过24个小时

这种做法避免了频繁的更新atime,提高了文件系统的性能。果然做Linux内核的大牛无不从每一个细节抓起呢,敬佩。
————————————————

获取精确的atime时间

但是在kernel 2.6.30之后,如果你的产品需要获取atime的精确时间呢?

OS启动的时候会读取/etc/fstab文件,对磁盘分区进行挂载我们可以添加strictatime选项:

UUID=d2a07167-d979-4cb8-a50e-dde36f4c7139/                       ext4    defaults,strictatime        1 1

但是这种做法需要重启OS,如果不重启OS我们可以使用remount(以挂载在”/”的文件系统为例):


mount -o remount,rw,strictatime /

这样挂载在”/”目录的文件系统就能够及时的更新atime了。
————————————————

原文地址:https://www.cnblogs.com/osker/p/12693809.html