Skip to content

Instantly share code, notes, and snippets.

@ginkgomzd
Last active September 27, 2018 16:42
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 ginkgomzd/050d1452709d5a7cddbd802e0c8087a3 to your computer and use it in GitHub Desktop.
Save ginkgomzd/050d1452709d5a7cddbd802e0c8087a3 to your computer and use it in GitHub Desktop.
poormans live-reload, or whatever, over and over and over
#!/usr/bin/env bash
# This is a gist
# https://gist.github.com/ginkgomzd/050d1452709d5a7cddbd802e0c8087a3
run-every() {
boldtxt=$(tput bold)
normaltxt=$(tput sgr0)
if [ "$#" -eq '0' ]; then
cat << HELPTXT
${boldtxt}NAME${normaltxt}
Run-Every - run a command indefinitely, sleeping after each run.
${boldtxt}SYNOPSIS${normaltxt}
run-every <script> [<sleep-time>]
${boldtxt}DESCRIPTION${normaltxt}
Executes a loop calling the passed argument and sleeping at the end of each loop.
Default sleep-time is 5.
See sleep man-page for time unit specifications (smhd). Default unit is seconds.
Scripts needing sudo should use non-interactive (-n). Script will offer to authenticate
before initiating looping.
${boldtxt}EXAMPLE${normaltxt}
$> run-every ./my-sync-cmd 30s
HELPTXT
return
fi
if [ ! -f $1 ]; then
echo "Could not find: $1"
return $?
fi
if [ ! -x $1 ]; then
echo "Please grant execute permissions on: $1"
return 1
fi
echo "Confirmed command: $1"
echo "All command output will be surpressed."
echo "It is recommended that scripts should run sudo non-interactive (-n)."
echo "Would you like to authenticate to sudo? [Y/n]"
read user
if [ "$user" == 'Y' ]; then
sudo pwd >/dev/null
fi
if [ -z $2 ]; then
INTERVAL='5s'
else
INTERVAL=$2
fi
echo -n "Looping every $INTERVAL:"
while :
do
"$1" >/dev/null
echo -n .
sleep $INTERVAL
done
}
run-every $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment