Created
July 24, 2013 16:36
-
-
Save filippog/6072201 to your computer and use it in GitHub Desktop.
# minimal wrapper to execute a command every given seconds (by default 60)
# useful to implement very basic rate-limiting when executing commands
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 | |
# minimal wrapper to execute a command every given seconds (by default 60) | |
# useful to implement very basic rate-limiting when executing commands | |
set -u | |
set -e | |
WAIT=${WAIT:-60} | |
STATEDIR=${STATEDIR:-/tmp/nofork} | |
[ -d "$STATEDIR" ] || mkdir "$STATEDIR" | |
cmdname=${@//\//_} | |
cmdfile=$STATEDIR/$cmdname | |
statcmd="stat --printf %Y" | |
[ $(uname -s) = "Darwin" ] && statcmd="stat -f %m -t %s" | |
if [ -e "$cmdfile" ]; then | |
mtime=$($statcmd -- "$cmdfile") | |
now=$(date +%s) | |
[ $(( $now - $mtime )) -lt $WAIT ] && exit 0 | |
rm -f -- "$cmdfile" | |
fi | |
touch "$cmdfile" | |
exec "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment