Skip to content

Instantly share code, notes, and snippets.

@sebble
Created February 6, 2017 13:03
Show Gist options
  • Save sebble/a7b26e95970edb7b6d27284829baa699 to your computer and use it in GitHub Desktop.
Save sebble/a7b26e95970edb7b6d27284829baa699 to your computer and use it in GitHub Desktop.
Run a shell script only once per time interval
#!/bin/sh
# Configuration
INTERVAL=10 # (seconds)
LASTRUN=".lastrun" # (optional)
# Run_per logic
NOW=$(date +%s)
test -f "$LASTRUN" && THEN=$(cat "$LASTRUN") || THEN=0
test $INTERVAL -gt $(($NOW - $THEN)) && exit 0
echo -n $NOW > "$LASTRUN"
# Your script below
# ...
#!/bin/sh
# Replace "10" with desired interval in seconds
[ -f .lastrun ]&&[ 10 -gt $((`date +%s`-`cat .lastrun`)) ]&&exit 0||date +%s>.lastrun
# Your script below
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment