Skip to content

Instantly share code, notes, and snippets.

@jayenashar
Created October 28, 2022 23:26
Show Gist options
  • Save jayenashar/a743b7c5e5bd9f669c5ebccc57b895b2 to your computer and use it in GitHub Desktop.
Save jayenashar/a743b7c5e5bd9f669c5ebccc57b895b2 to your computer and use it in GitHub Desktop.
throttle in bash
#!/bin/bash
# usage: throttle.sh FILENAME THROTTLE_SECONDS CMD [ARGS...]
# example usage: while sleep .1; do throttle.sh /tmp/date 1 date +%s.%N; done
FILENAME=$1
THROTTLE_SECONDS=$2
shift 2
last_called=$(<$FILENAME)
if [[ -z "$last_called" ]]; then
last_called=$EPOCHSECONDS
fi
if ((EPOCHSECONDS - last_called >= THROTTLE_SECONDS)); then
"$@"
fi
echo $EPOCHSECONDS > $FILENAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment