Created
October 28, 2022 23:26
-
-
Save jayenashar/a743b7c5e5bd9f669c5ebccc57b895b2 to your computer and use it in GitHub Desktop.
throttle in bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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