From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 25149 invoked by alias); 29 Dec 2012 23:35:04 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 17504 Received: (qmail 13634 invoked from network); 29 Dec 2012 23:35:02 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 Received-SPF: none (ns1.primenet.com.au: domain at closedmail.com does not designate permitted sender hosts) From: Bart Schaefer Message-id: <121229153449.ZM7616@torch.brasslantern.com> Date: Sat, 29 Dec 2012 15:34:49 -0800 In-reply-to: Comments: In reply to TJ Luoma "only run if X seconds have elapsed (time differences in seconds)" (Dec 29, 2:40pm) References: X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: Zsh-Users List Subject: Re: only run if X seconds have elapsed (time differences in seconds) MIME-version: 1.0 Content-type: text/plain; charset=us-ascii 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".