Skip to content

Instantly share code, notes, and snippets.

@dvapelnik
Last active August 29, 2015 14:22
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 dvapelnik/3c0f0b9b7909be8f63de to your computer and use it in GitHub Desktop.
Save dvapelnik/3c0f0b9b7909be8f63de to your computer and use it in GitHub Desktop.
Run the command several times per minute
#!/bin/bash
# Script can start few command instances in one minute
# with automatic sleep and count per second
# Created for run multiple commands each mimute with cron on linux OS
# Author: Dmitry Vapelnik <dvapelnik@gmail.com>
# Created: 2015-06-01 23:26:43
function help {
HELP="You must use two arguments: first - how many time to execute command in next minute; second - commant to execute"
echo -e $HELP
}
# Show help message
if [[ "$1" = "--help" || "$1" = "-h" ]]; then help; exit 0; fi
# Check count of arguments
if [[ ! "$#" = "2" ]]; then
echo You must specify count of executions per minute and command
exit 1
fi
let "c = 60 % $1"
# Calculate sleep for each command
if [[ "$c" = "0" ]]; then
COUNT=$1
let "SLEEP = 60 / $1"
else
echo "60 % $1: 60 is not divisible by the value of the first argument"
exit 1
fi
for ((n=0;n<$COUNT;n++))
do
bash -c "$2" &
sleep $SLEEP
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment