One of the things which confuses many Linux users is why the access time attribute of a file does not change, although the file has been clearly accessed a number of times recently. Let me illustrate here by accessing a file, and checking whether the access time changes or not. I will use
stat -c %x filename
to grab the atime attribue.
[root@ip-10-136-87-176 lvm]# sleep 10; date; cat myfile ; stat -c %x myfile Sun Aug 3 20:56:51 UTC 2014 Beam me up, Scotty. 2014-08-03 20:54:40.000000000 +0000 [root@ip-10-136-87-176 lvm]# sleep 10; date; cat myfile ; stat -c %x myfile Sun Aug 3 20:57:23 UTC 2014 Beam me up, Scotty. 2014-08-03 20:54:40.000000000 +0000
The atime has not changed. Let us check
/proc/mounts
for any mount options.
[root@ip-10-136-87-176 lvm]# pwd /mnt/lvm [root@ip-10-136-87-176 lvm]# grep /mnt/lvm /proc/mounts /dev/xvdj1 /mnt/lvm ext3 rw,seclabel,relatime,errors=continue,barrier=1,data=ordered 0 0
The answer to our question lies in the
relatime
option.The Linux Kernel starting from version 2.6.30 switched to using the relatime by default during file system mount. Here is the exerpts from the man page for mount command –
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 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 addi- tion, since Linux 2.6.30, the file’s last access time is always updated if it is more than 1 day old.
If the Kernel was to update the atime everytime a file was accessed that would be a big performance killer for disks. Specially in servers with lots of files which are accessed frequently, updating the atime attribute everytime a file is accessed would be a huge I/O burden, that is why the Kernel defaults to relatime. But as always, the Linux Kernel provides you the mechanism to update the atime everytime a file is accessed if you want to. For this to work you can use the
strictatime
option during mount. Let me illustrate this –
[root@ip-10-136-87-176 /]# umount /mnt/lvm [root@ip-10-136-87-176 /]# mount -o strictatime /dev/xvdj1 /mnt/lvm/ [root@ip-10-136-87-176 /]# grep '/mnt/lvm' /proc/mounts /dev/xvdj1 /mnt/lvm ext3 rw,seclabel,errors=continue,barrier=1,data=ordered 0 0 [root@ip-10-136-87-176 /]# cd /mnt/lvm/ [root@ip-10-136-87-176 lvm]# sleep 10; date; cat myfile ; stat -c %x myfile Sun Aug 3 21:06:22 UTC 2014 Beam me up, Scotty. 2014-08-03 21:06:22.000000000 +0000 [root@ip-10-136-87-176 lvm]# sleep 60; date; cat myfile ; stat -c %x myfile Sun Aug 3 21:07:27 UTC 2014 Beam me up, Scotty. 2014-08-03 21:07:27.000000000 +0000
Note: If the file system is mounted with a readonly option, the atime won’t be updated for obvious reasons.