zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh-Users List <zsh-users@zsh.org>
Subject: Re: only run if X seconds have elapsed (time differences in seconds)
Date: Sat, 29 Dec 2012 15:34:49 -0800	[thread overview]
Message-ID: <121229153449.ZM7616@torch.brasslantern.com> (raw)
In-Reply-To: <CADjGqHtgatYEHyus2ru8vUPUJJQH48nGNVH8XbvV11nr0HhpGg@mail.gmail.com>

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".


  reply	other threads:[~2012-12-29 23:35 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-29 19:40 TJ Luoma
2012-12-29 23:34 ` Bart Schaefer [this message]
2012-12-30  1:00   ` TJ Luoma

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=121229153449.ZM7616@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).