Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: only run if X seconds have elapsed (time differences in seconds)



On Dec 29,  2:40pm, TJ Luoma wrote:
}
} I very often find myself wanting to say "Run this command, but only if
} it has not been run in the past X seconds.
} 
} Is there an easier / better way to do this than the way that I am
} doing it? If so, what would you recommend?

You need to record the last time it was run somewhere, so in the file
system is as good a place as any.

You might avoid loading zsh/datetime and doing your own arithmetic by
using file modification time with the "m" glob qualifier.

    RUN_EVERY=86400

    setopt globassign
    LASTRUN=~/.lastrun
    RAN_RECENTLY=$LASTRUN(Nms-$RUN_EVERY)

    [[ -n $RAN_RECENTLY ]] && exit 0
    : >| $LASTRUN

One other note -- there is a potential race condition during the short
time between doing the glob and writing to the file.  Two runs of the
script that are close enough to simultaneous might both find the file
to be "old enough".



Messages sorted by: Reverse Date, Date, Thread, Author