7

When you run ./myscript.sh is that considered as "access" time?

I need to know the last time a script was run, but I'm not sure if this counts as mtime, ctime or atime (differences described here).

Andrejs
  • 315
  • 2
  • 4
  • 10

1 Answers1

17

As explained in the answer you linked to, that depends on your settings. In principle, atime will change each time a file is read and in order to run a script, you need to read it. So yes, normally, the atime will change each time the script is executed. This is easily demonstrated by checking the current atime, running the script and then checking it again:

$ 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

However, if the script resides on a filesystem that is mounted with the noatime or relatime options (or any of the other possible options that can affect how atime is modified), the behavior will be different:

   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.

You can check what options your mounted systems are using by running the command mount with no arguments. The tests I show above were run on a filesystem that was mounted using the relatime option. With this option, atime is updated if i) the current atime is older than the current modification or change time or ii) it is hasn't been updated for more than a day.

So, on a system with relatime, the atime is not changed when a file is accessed if the current atime is newer than the current modification time:

$ 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

The atime is always changed on access if it is more than a day old. Even if the modification time is older:

$ 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

So, on most modern Linux systems, atime will only be updated every day or so, unless the file has been modified since it was last accessed.

terdon
  • 242,166