Skip to content

Instantly share code, notes, and snippets.

@alexfouche
Last active January 4, 2020 19:22
Show Gist options
  • Save alexfouche/8287054ba8a5fb248b3fe3a04a8081de to your computer and use it in GitHub Desktop.
Save alexfouche/8287054ba8a5fb248b3fe3a04a8081de to your computer and use it in GitHub Desktop.
Run a command every X seconds. Differs from `watch` because it does not clear the screen at each command run
#!/bin/bash
# NOTE: This requires GNU getopt. On Mac OS X and FreeBSD, you have to install this
# separately; see below.
OPTS=`getopt -o hn: --long help,interval: -n 'parse-options' -- "$@"`
if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi
# echo "$OPTS"
# Note the quotes around `$OPTS': they are essential!
eval set -- "$OPTS"
want_help=false
interval=10
while true; do
case "$1" in
-h | --help ) want_help=true; shift ;;
-n | --interval ) interval="$2"; shift; shift ;;
-- ) shift; break ;;
* ) break ;;
esac
done
if test "${want_help}" == "true"; then
echo "Usage: [-n|--interval <interval>] [-h]"
echo " -h|--help: print help"
echo " -n|--interval: sleep interval in seconds"
exit 0
fi
if test "$interval" -le 1; then
echo "invalid interval=$interval"
exit -1
fi
###
# DO THE WORK
###
while true; do
echo
date
"$@"
sleep $interval
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment