* only run if X seconds have elapsed (time differences in seconds)
@ 2012-12-29 19:40 TJ Luoma
2012-12-29 23:34 ` Bart Schaefer
0 siblings, 1 reply; 3+ messages in thread
From: TJ Luoma @ 2012-12-29 19:40 UTC (permalink / raw)
To: Zsh-Users List
I very often find myself wanting to say "Run this command, but only if
it has not been run in the past X seconds.
For example, if I wanted to run something no more often than once per
day, I'd do something like this:
#!/bin/zsh
# load this so I can use EPOCHSECONDS
zmodload zsh/datetime
# this is a minimum of how many seconds should elapse between runs
RUN_EVERY=86400
# this is a text file where the previous timestamp is saved
LASTRUN=$HOME/.lastrun
# if there is no LASTRUN file, make it zero
[[ -e "$LASTRUN" ]] || echo -n 0 > "$LASTRUN"
# save current time
NOW=$EPOCHSECONDS
# get previous time, and strip out anything that is not a digit
THEN=$(tr -dc '[0-9]' < "$LASTRUN" )
# get the difference in seconds between NOW and THEN (the last time it was run
DIFF=$(($NOW - $THEN))
# if the difference is less than RUN_EVERY then exit
[[ "$DIFF" -le "$RUN_EVERY" ]] && exit 0
# if we get here, it's time to run again, so let's update LASTRUN:
echo -n "$NOW" > $LASTRUN
# And then I do whatever else the script is supposed to do here
My question is:
Is there an easier / better way to do this than the way that I am
doing it? If so, what would you recommend?
Thanks!
TjL
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: only run if X seconds have elapsed (time differences in seconds)
2012-12-29 19:40 only run if X seconds have elapsed (time differences in seconds) TJ Luoma
@ 2012-12-29 23:34 ` Bart Schaefer
2012-12-30 1:00 ` TJ Luoma
0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2012-12-29 23:34 UTC (permalink / raw)
To: Zsh-Users List
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".
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: only run if X seconds have elapsed (time differences in seconds)
2012-12-29 23:34 ` Bart Schaefer
@ 2012-12-30 1:00 ` TJ Luoma
0 siblings, 0 replies; 3+ messages in thread
From: TJ Luoma @ 2012-12-30 1:00 UTC (permalink / raw)
To: Bart Schaefer; +Cc: Zsh-Users List
On Sat, Dec 29, 2012 at 6:34 PM, Bart Schaefer
<schaefer@brasslantern.com> wrote:
> 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".
True enough, but I intend to run this via `launchd` every ~4 hours. I
don't want it to run more often than once a day, but since the
computer might be asleep at times, I figured doing a "how long since
this last ran?" check was a good alternative.
TjL
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2012-12-30 2:07 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-12-29 19:40 only run if X seconds have elapsed (time differences in seconds) TJ Luoma
2012-12-29 23:34 ` Bart Schaefer
2012-12-30 1:00 ` TJ Luoma
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).