Skip to content

Instantly share code, notes, and snippets.

@keylase
Last active May 23, 2021 15:22
Show Gist options
  • Save keylase/4d37ad2931af8ed8315acc9f9d2404f5 to your computer and use it in GitHub Desktop.
Save keylase/4d37ad2931af8ed8315acc9f9d2404f5 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
@keylase
Copy link
Author

keylase commented Jan 23, 2017

*/1 * * * * ~/bin/runEvery.sh 5 otherScript.sh

@AndreasGrip
Copy link

Sorry but I cant for my life figure out how to post a change request.

Fork with timedrift fix

Instead of this, it handles the time drift that is created by the program while it runs. For instance if your program run for 4seconds and you want to run it each second. This original will run 60times, once for each second. 60 * 5 = 300. This will make your system have 5 sets of processes run at each time, NOT what you wanted.
My fix is not perfect, but it's a huge improvement.

@keylase if you still use this I assume you want to incorporate the changes I made, I assume you will not have any problem understand what the code does.

@keylase
Copy link
Author

keylase commented May 23, 2021

Sorry but I cant for my life figure out how to post a change request.

Fork with timedrift fix

Instead of this, it handles the time drift that is created by the program while it runs. For instance if your program run for 4seconds and you want to run it each second. This original will run 60times, once for each second. 60 * 5 = 300. This will make your system have 5 sets of processes run at each time, NOT what you wanted.
My fix is not perfect, but it's a huge improvement.

@keylase if you still use this I assume you want to incorporate the changes I made, I assume you will not have any problem understand what the code does.

Thank you! it looks good - fixed 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment