Skip to content

Instantly share code, notes, and snippets.

@filippog
Created July 24, 2013 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filippog/6072201 to your computer and use it in GitHub Desktop.
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
#!/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