检查上次使用的 .sh 文件

检查上次使用的 .sh 文件

当你跑步时,./myscript.sh这被认为是“访问”时间吗?

我需要知道上次运行脚本的时间,但我不确定这是否算作mtimectimeatime(描述的差异这里)。

答案1

正如中所解释的答案您链接到的,这取决于您的设置。原则上,atime每次文件被修改时都会改变为了运行脚本,您需要阅读它。所以是的,通常情况下,atime每次执行脚本时都会改变。这可以通过检查当前的 atime、运行脚本然后再次检查来轻松演示:

$ printf '#!/bin/sh\necho "running"\n' > ~/myscript.sh
$ stat -c '%n : %x' ~/myscript.sh 
/home/terdon/myscript.sh : 2016-02-23 10:36:49.349656971 +0200

$ chmod 700 ~/myscript.sh 
$ stat -c '%n : %x' ~/myscript.sh  ## This doesn't change atime
/home/terdon/myscript.sh : 2016-02-23 10:36:49.349656971 +0200

$ ~/myscript.sh 
running
$ stat -c '%n : %x' ~/myscript.sh   ## Running the script does
/home/terdon/myscript.sh : 2016-02-23 10:38:20.954893580 +0200

但是,如果脚本驻留在使用noatimerelatime选项(或任何其他可能影响atime修改方式的选项)挂载的文件系统上,则行为将有所不同:

   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 implies nodira‐
          time.

   relatime
          Update inode access times relative to  modify  or  change  time.
          Access time is only updated if the previous access time was ear‐
          lier than the current modify or change time.  (Similar to  noat‐
          ime,  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 modi‐
          fied.)

          Since Linux 2.6.30, the kernel defaults to the behavior provided
          by this option (unless noatime was specified), and the  stricta‐
          time  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.

mount您可以通过运行不带参数的命令来检查已安装的系统正在使用哪些选项。我上面显示的测试是在使用该relatime选项安装的文件系统上运行的。使用此选项,atime如果 i) 当前atime早于当前修改或更改时间或 ii) 超过一天没有更新,则更新。

因此,在具有 的系统上relatime,如果当前时间比当前修改时间新,则atime访问文件时 不会更改:atime

$ touch -ad "+2 days" file
$ stat --printf 'mtime: %y\natime: %x\n' file
mtime: 2016-02-23 11:01:53.312350725 +0200
atime: 2016-02-25 11:01:53.317432842 +0200
$ cat file 
$ stat --printf 'mtime: %y\natime: %x\n' file
mtime: 2016-02-23 11:01:53.312350725 +0200
atime: 2016-02-25 11:01:53.317432842 +0200

atime如果超过一天,访问时总是会更改。即使修改时间较旧:

$ touch -ad "-2 days" file
$ touch -md "-4 days" file
$ stat --printf 'mtime: %y\natime: %x\n' file
mtime: 2016-02-19 11:03:59.891993606 +0200
atime: 2016-02-21 11:03:37.259807129 +0200
$ cat file
$ stat --printf 'mtime: %y\natime: %x\n' file
mtime: 2016-02-19 11:03:59.891993606 +0200
atime: 2016-02-23 11:05:17.783535537 +0200

因此,在大多数现代 Linux 系统上,atime只会每天左右更新一次,除非文件自上次访问以来已被修改。

相关内容