• script to run in background and log access times to a file

    From J. W.@21:1/5 to All on Sun Mar 13 12:52:40 2022
    Hello, I am a user on an institutional system. Could someone point me in the right direction to a command I can execute with nohup to check last access time to a given file in the background, and write the times to a log file? Thank you!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to J. W. on Sun Mar 13 21:36:35 2022
    On 13.03.2022 20:52, J. W. wrote:
    Hello, I am a user on an institutional system. Could someone point me
    in the right direction to a command I can execute with nohup to check
    last access time to a given file in the background, and write the
    times to a log file? Thank you!

    It depends on what system you work on. On my Linux system there's a
    tool called 'inotifywait' (use 'man inotifywait' to get details).
    inotifywait - wait for changes to files using inotify

    Here's an application example - but different from your case; it is
    watching in a specific directory all create/delete changes of files
    matching a particular file name pattern, and it displays information
    about that through an alert box -, that may help you (or not):

    #!/usr/bin/ksh
    dir=/var/games/slashem
    inotifywait -q -m -e create -e delete "${dir}" |
    while read -r WHERE EVENT FILE
    do
    case ${FILE} in
    (bon*.*.gz)
    typeset -l event=${EVENT}
    zenity --info --text="File '${FILE}' ${event}d\n(in ${WHERE})\n"
    ;;
    esac
    done

    and with 'inotifywait' you can also watch specific files' changes
    with access time events; the man page provides you with details.

    If you have further questions feel free to ask.

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bit Twister@21:1/5 to J. W. on Sun Mar 13 16:14:31 2022
    On Sun, 13 Mar 2022 12:52:40 -0700 (PDT), J. W. wrote:
    Hello, I am a user on an institutional system. Could someone point me in the right direction to a command I can execute with nohup to check last access time to a given file in the background, and write the times to a log file? Thank you!

    redirection operator seems appropriate for the append requirement.

    It always helps if you provide information about your system, desktop, operating
    system, hardware when posting questions. That allows any subject matter expert to
    give you more detailed information in a reply.


    File information can be found with stat.
    homework:
    man stat
    man cut
    man grep
    man tail

    as for writing to a log file, it depends on script/shell/app being used.

    Do bookmark the following url
    https://www.google.com/advanced_search


    Example bash script
    ----8<----8<----8<----8<--cut below this line--8<----8<----8<
    #!/bin/bash
    #****************************************************************
    #* script_name_here - script to fetch access time of a file #****************************************************************
    set -u # Show usage of unset variables as an error to help catch typos.

    _in_fn=/somewhere/FILENAME_OF_INTEREST_HERE
    _log_fn="$HOME/DATA/access.log"

    #*************************************************************
    #* not really needed/required because >> redirection operator
    #* will create file if it does not exist.
    #*************************************************************
    mkdir --parents $HOME/DATA/
    touch $_log_fn # creates initial empty file if does not exist

    echo "\
    stat %x $_in_fn | grep Access: | tail -1
    " >> $_log_fn

    #**************** end of bash script ******************************** ----8<----8<----8<----8<--cut above this line--8<----8<----8<

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David W. Hodgins@21:1/5 to J. W. on Sun Mar 13 16:45:00 2022
    On Sun, 13 Mar 2022 15:52:40 -0400, J. W. <jw2.685452@gmail.com> wrote:

    Hello, I am a user on an institutional system. Could someone point me in the right direction to a command I can execute with nohup to check last access time to a given file in the background, and write the times to a log file? Thank you!

    See https://unix.stackexchange.com/questions/12247/linux-file-access-monitoring

    Regards, Dave Hodgins

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From J. W.@21:1/5 to Janis Papanagnou on Tue Mar 15 07:43:51 2022
    On Sunday, March 13, 2022 at 4:36:42 PM UTC-4, Janis Papanagnou wrote:
    On 13.03.2022 20:52, J. W. wrote:
    Hello, I am a user on an institutional system. Could someone point me
    in the right direction to a command I can execute with nohup to check
    last access time to a given file in the background, and write the
    times to a log file? Thank you!
    It depends on what system you work on. On my Linux system there's a
    tool called 'inotifywait' (use 'man inotifywait' to get details). inotifywait - wait for changes to files using inotify

    Here's an application example - but different from your case; it is
    watching in a specific directory all create/delete changes of files
    matching a particular file name pattern, and it displays information
    about that through an alert box -, that may help you (or not):

    #!/usr/bin/ksh
    dir=/var/games/slashem
    inotifywait -q -m -e create -e delete "${dir}" |
    while read -r WHERE EVENT FILE
    do
    case ${FILE} in
    (bon*.*.gz)
    typeset -l event=${EVENT}
    zenity --info --text="File '${FILE}' ${event}d\n(in ${WHERE})\n"
    ;;
    esac
    done

    and with 'inotifywait' you can also watch specific files' changes
    with access time events; the man page provides you with details.

    If you have further questions feel free to ask.

    Janis

    Thank you very much Janis. I ended up having to do something I could understand from scratch myself (and I know very little), but it seems to have worked. I wrote this script into a file, made it executable, then ran it from a detached tmux session and
    it is still going. Since I only need very basic information on time access, do you think this will work just as well as your expert solution? It seemed clunky to have to use a temporary file instead of uniq'ing in place, but it all seems to work.

    while true
    do
    ls -lu ~/myfile | cut -d ' ' -f 6-9 >> ~/sm_log
    uniq ~/sm_log > ~/sm_log_t
    mv ~/sm_log_t ~/sm_log
    sleep 120
    done

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to J. W. on Tue Mar 15 15:17:41 2022
    In article <0ed022ce-2770-4b58-b937-3e3bbee21dadn@googlegroups.com>,
    J. W. <jw2.685452@gmail.com> wrote:
    ...
    Thank you very much Janis. I ended up having to do something I could >understand from scratch myself (and I know very little), but it seems
    to have worked. I wrote this script ...

    Although it may look daunting at first, inotifywait (and related tools) is really worth learning. It solves a lot of these sorts of problems, where
    the "normal" way of doing it is poll-sleep-repeat. So, I would recommend
    that you install it and learn it.

    Janis's script uses the "-m" (monitor) option in inotifywait, which is
    kinda cool, but is usually not the best way to go at it. My scripts
    usually take the form of:

    while :;do
    inotifywait ...
    do stuff
    done

    That is, I just use inotifywait to wait until something happens, then I do
    my own code once it has happened. I recommend that you do likewise - that
    you re-write your script to use inotifywait (without -m) and use the
    experience to ease yourself into learning about it.

    Note also that there is a tool called inotify-hookable, that is actually a
    Perl script, that sort of automates this process. It combines the functionality of inotifywait's "-m" option with the idea of "Run some user-specified code whenever the event(s) happen". I'd install and play
    with that as well.

    # Clunky script follows...
    while true
    do
    ls -lu ~/myfile | cut -d ' ' -f 6-9 >> ~/sm_log
    uniq ~/sm_log > ~/sm_log_t
    mv ~/sm_log_t ~/sm_log
    sleep 120
    done

    --
    The randomly chosen signature file that would have appeared here is more than 4 lines long. As such, it violates one or more Usenet RFCs. In order to remain in compliance with said RFCs, the actual sig can be found at the following URL:
    http://user.xmission.com/~gazelle/Sigs/IceCream

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Kenny McCormack on Tue Mar 15 16:29:05 2022
    On 15.03.2022 16:17, Kenny McCormack wrote:
    In article <0ed022ce-2770-4b58-b937-3e3bbee21dadn@googlegroups.com>,
    J. W. <jw2.685452@gmail.com> wrote:
    ...
    Thank you very much Janis. I ended up having to do something I could
    understand from scratch myself (and I know very little), but it seems
    to have worked. I wrote this script ...

    Although it may look daunting at first, inotifywait (and related tools) is really worth learning. It solves a lot of these sorts of problems, where
    the "normal" way of doing it is poll-sleep-repeat. So, I would recommend that you install it and learn it.

    Janis's script uses the "-m" (monitor) option in inotifywait, which is
    kinda cool, but is usually not the best way to go at it.

    It depends on what you intend to do. In my sample code the poll was what
    I needed. And I read the OP's demand that the "control structure" would
    also match for his case.

    The logic you applied below resembles the select() code pattern (which certainly has its own field if use cases).

    I would use this pattern if (for example) I have an exit condition to
    be triggered at some point (e.g. a target date is reached, a number of interesting events reached some threshold, etc.).

    Janis

    My scripts
    usually take the form of:

    while :;do
    inotifywait ...
    do stuff
    done

    That is, I just use inotifywait to wait until something happens, then I do
    my own code once it has happened. I recommend that you do likewise - that you re-write your script to use inotifywait (without -m) and use the experience to ease yourself into learning about it.

    Note also that there is a tool called inotify-hookable, that is actually a Perl script, that sort of automates this process. It combines the functionality of inotifywait's "-m" option with the idea of "Run some user-specified code whenever the event(s) happen". I'd install and play
    with that as well.

    # Clunky script follows...
    while true
    do
    ls -lu ~/myfile | cut -d ' ' -f 6-9 >> ~/sm_log
    uniq ~/sm_log > ~/sm_log_t
    mv ~/sm_log_t ~/sm_log
    sleep 120
    done


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to J. W. on Tue Mar 15 16:21:34 2022
    On 15.03.2022 15:43, J. W. wrote:
    On Sunday, March 13, 2022 at 4:36:42 PM UTC-4, Janis Papanagnou wrote:
    On 13.03.2022 20:52, J. W. wrote:
    Hello, I am a user on an institutional system. Could someone point me
    in the right direction to a command I can execute with nohup to check
    last access time to a given file in the background, and write the
    times to a log file? Thank you!
    It depends on what system you work on. On my Linux system there's a
    tool called 'inotifywait' (use 'man inotifywait' to get details).
    inotifywait - wait for changes to files using inotify

    Here's an application example - but different from your case; it is
    watching in a specific directory all create/delete changes of files
    matching a particular file name pattern, and it displays information
    about that through an alert box -, that may help you (or not):

    #!/usr/bin/ksh
    dir=/var/games/slashem
    inotifywait -q -m -e create -e delete "${dir}" |
    while read -r WHERE EVENT FILE
    do
    case ${FILE} in
    (bon*.*.gz)
    typeset -l event=${EVENT}
    zenity --info --text="File '${FILE}' ${event}d\n(in ${WHERE})\n"
    ;;
    esac
    done

    and with 'inotifywait' you can also watch specific files' changes
    with access time events; the man page provides you with details.

    If you have further questions feel free to ask.

    Janis

    Thank you very much Janis. I ended up having to do something I could understand from scratch myself (and I know very little), but it seems to
    have worked. I wrote this script into a file, made it executable, then
    ran it from a detached tmux session and it is still going. Since I only
    need very basic information on time access, do you think this will work
    just as well as your expert solution? It seemed clunky to have to use a temporary file instead of uniq'ing in place, but it all seems to work.

    There's certainly some comments worth regarding your script below. But
    I would first make sure whether there are better options available on
    your system. Since _if_ you have the tool I mentioned ('inotifywait')
    then a solution to check the events for a file might be as simple as

    inotifywait -q -m --timefmt '%F %T' --format '%T %e %w' your_file |
    tee your_file.log

    (Note: I didn't redirect the output to your logfile directly just to
    see also on the terminal what will be logged. Change that as desired.)

    This tool has the advantage that you immediately get a notification
    when the file attributes change (as opposed to implementing a "busy
    wait" loop for 2 minutes, unnecessarily fetching the same info many
    times, just to clean that up again.).



    while true
    do
    ls -lu ~/myfile | cut -d ' ' -f 6-9 >> ~/sm_log

    The 'ls' default date format is unreliable if you want to parse it.

    You can use the 'stat' command to obtain the infos about the file.

    uniq ~/sm_log > ~/sm_log_t

    I'd compare the 'stat' result to the previous result; no temporaries.

    mv ~/sm_log_t ~/sm_log
    sleep 120

    Waiting and time-triggered polling is rarely a good solution.

    done


    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bit Twister@21:1/5 to Janis Papanagnou on Tue Mar 15 17:28:06 2022
    On Tue, 15 Mar 2022 16:21:34 +0100, Janis Papanagnou wrote:
    On 15.03.2022 15:43, J. W. wrote:
    On Sunday, March 13, 2022 at 4:36:42 PM UTC-4, Janis Papanagnou wrote:
    On 13.03.2022 20:52, J. W. wrote:
    Hello, I am a user on an institutional system. Could someone point me
    in the right direction to a command I can execute with nohup to check
    last access time to a given file in the background, and write the
    times to a log file? Thank you!
    It depends on what system you work on. On my Linux system there's a
    tool called 'inotifywait' (use 'man inotifywait' to get details).
    inotifywait - wait for changes to files using inotify

    Here's an application example - but different from your case; it is
    watching in a specific directory all create/delete changes of files
    matching a particular file name pattern, and it displays information
    about that through an alert box -, that may help you (or not):

    #!/usr/bin/ksh
    dir=/var/games/slashem
    inotifywait -q -m -e create -e delete "${dir}" |
    while read -r WHERE EVENT FILE
    do
    case ${FILE} in
    (bon*.*.gz)
    typeset -l event=${EVENT}
    zenity --info --text="File '${FILE}' ${event}d\n(in ${WHERE})\n"
    ;;
    esac
    done

    and with 'inotifywait' you can also watch specific files' changes
    with access time events; the man page provides you with details.

    If you have further questions feel free to ask.

    Janis

    Thank you very much Janis. I ended up having to do something I could
    understand from scratch myself (and I know very little), but it seems to
    have worked. I wrote this script into a file, made it executable, then
    ran it from a detached tmux session and it is still going. Since I only
    need very basic information on time access, do you think this will work
    just as well as your expert solution? It seemed clunky to have to use a
    temporary file instead of uniq'ing in place, but it all seems to work.

    There's certainly some comments worth regarding your script below. But
    I would first make sure whether there are better options available on
    your system. Since _if_ you have the tool I mentioned ('inotifywait')
    then a solution to check the events for a file might be as simple as

    inotifywait -q -m --timefmt '%F %T' --format '%T %e %w' your_file |
    tee your_file.log

    I find using "script" more productive than tee. Usually as simple as
    script -c script_of_interest_here log_fn_here
    To troubleshoot I use
    script -c "bash -x ; script_of_interest_here" log_fn_here

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)