Skip to content

Instantly share code, notes, and snippets.

@AndreasGrip
Forked from keylase/runEvery.sh
Last active May 20, 2021 09:24
Show Gist options
  • Save AndreasGrip/1a9f8f6e08e235b5c3b6a8a47ba2a8a9 to your computer and use it in GitHub Desktop.
Save AndreasGrip/1a9f8f6e08e235b5c3b6a8a47ba2a8a9 to your computer and use it in GitHub Desktop.
#!/bin/bash
inputPeriod=$1
runCommand=$2
RUN_TIME=60
RUN_TIME_LEFT=1
error="no"
SECONDS=0
if [ 'x'"$runCommand" != 'x' ]
then
if [ 'x'$inputPeriod != 'x' ]
then
loops=$(( $RUN_TIME / $inputPeriod ))
if [ $loops -eq 0 ]
then
loops=1
fi
for i in $(eval echo {1..$loops})
do
$runCommand
sleep $inputPeriod
RUN_TIME_LEFT=$((RUN_TIME-SECONDS))
if [ $RUN_TIME_LEFT -le 0 ]
then
break
fi
done
else
error="yes"
fi
else
error="yes"
fi
if [ $error = "yes" ]
then
echo "runEvery - runs a command every X seconds for a minute"
echo "Usage: runEvery.sh <# in seconds < 60> <command to run>"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment