From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 14718 invoked by alias); 29 Dec 2012 20:43:37 -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: 17503 Received: (qmail 12323 invoked from network); 29 Dec 2012 20:43:35 -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.6 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,FREEMAIL_FROM,NML_ADSP_CUSTOM_MED,RCVD_IN_DNSWL_LOW, T_DKIM_INVALID autolearn=no version=3.3.2 Received-SPF: pass (ns1.primenet.com.au: SPF record at _netblocks.google.com designates 74.125.83.43 as permitted sender) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to:content-type; bh=nL0nbhzEcZDA46zkbMQxOeY8d8DsRmu61qLFe/9pfcI=; b=cF6c9DLCeVyBpVv+s99OSItF3+Lrh16GQkSHE0Be+UZIZHDVeEHNXCV7UyocGW11n8 dW9wZnVG3lxxO+gqGllwswxCl/ggkVIbrVRo09fPWZT302QduwTzUFGXk15mQYHeVxob V+I74oMHWEZVr4JSxtjGzwXuiNQkOJIBdcj7H8t5CDcz21HlrDM6tPloZUiK+41aIW7Y tbRsVv16GEnMeSYZcAr7THfS2PEjGCh1Igb9HmpbF/ZtkxAn5v1aGuupcunCWET5+nlV AXVSAaf/TJCy0tjplzIJKq6Xs7W6rpBIy/6t9C1g5rys4a5QbTFHWrQreKHq3ngD87rk TglA== MIME-Version: 1.0 From: TJ Luoma Date: Sat, 29 Dec 2012 14:40:11 -0500 Message-ID: Subject: only run if X seconds have elapsed (time differences in seconds) To: Zsh-Users List Content-Type: text/plain; charset=UTF-8 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