Trying to understand some interesting behavior when extracting last modification time from files in python. It seems like python does not properly refresh st_mtime (from certain files) or I am doing something very wrong.
My environment:
# python36
Python 3.6.1 (default, May 11 2017, 13:02:16)
[GCC 6.3.1 20161221 (Red Hat 6.3.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import datetime # this is just to see when was the command executed
My problem:
>>> print(datetime.datetime.now(), " --- ", os.stat('/proc/mounts').st_mtime)
2017-08-07 22:54:18.881310 --- 1502135080.9200804
>>> print(datetime.datetime.now(), " --- ", os.stat('/proc/mounts').st_mtime)
2017-08-07 22:54:20.597349 --- 1502135080.9200804
>>> print(datetime.datetime.now(), " --- ", os.stat('/proc/mounts').st_mtime)
2017-08-07 22:54:22.755422 --- 1502135080.9200804
As you can see, I am running os.stat('/proc/mounts').st_mtime command with a time interval around 2 seconds, but last modification time of /proc/mounts remains the same (epoch time is static 1502135080.9200804)
Just to prove that python is able to extract last modification time of other similar files:
>>> print(datetime.datetime.now(), " --- ", os.stat('/proc/vmstat').st_mtime)
2017-08-07 22:57:30.064103 --- 1502135850.0637884
>>> print(datetime.datetime.now(), " --- ", os.stat('/proc/vmstat').st_mtime)
2017-08-07 22:57:32.126273 --- 1502135852.1257987
>>> print(datetime.datetime.now(), " --- ", os.stat('/proc/vmstat').st_mtime)
2017-08-07 22:57:34.409254 --- 1502135854.4078097
In the example above, last modification time of another file is properly updated.
The file which I am trying to examine is /proc/mounts, which (in my case at least) is a symbolic link to /proc/self/mounts, but even if I try to extract the metadata of real file, last modification is still not updated.
One more thing: this file is indeed updated (by kernel?) very frequently. Here is a small shell snipped to prove the file last modification date is being constantly updated:
# [130] > while true; do ls -l --time-style=full-iso /proc/mounts; sleep 1; done
lrwxrwxrwx. 1 root root 11 2017-08-07 23:02:37.139422559 +0300 /proc/mounts -> self/mounts
lrwxrwxrwx. 1 root root 11 2017-08-07 23:02:38.143428493 +0300 /proc/mounts -> self/mounts
lrwxrwxrwx. 1 root root 11 2017-08-07 23:02:39.148434433 +0300 /proc/mounts -> self/mounts
lrwxrwxrwx. 1 root root 11 2017-08-07 23:02:40.152440367 +0300 /proc/mounts -> self/mounts
Any suggestions to what is happening when python extracts last modification time of /proc/mounts? It seems to me python is caching the value.