Skip to content

Instantly share code, notes, and snippets.

@jaysoncena
Created March 19, 2016 05:35
Show Gist options
  • Save jaysoncena/ff0e3cc672a519f0e9b9 to your computer and use it in GitHub Desktop.
Save jaysoncena/ff0e3cc672a519f0e9b9 to your computer and use it in GitHub Desktop.
Execute list of commands in parallel with limit on the number of concurrent commands
#!/bin/bash -x
OLD_IFS="${IFS}"
IFS="
"
LIMIT=2
cmd_buff="`cat command_list.txt`"
declare -a cmd_list=($cmd_buff)
for line in "${cmd_list[@]}"; do
echo ">> "$line
done
cmd_index=0
job_list=
while [ $cmd_index -lt ((${#cmd_list[@]}-1)) ]; do
echo "Running command #"$((cmd_index+1))
bash -xc "${cmd_list[$cmd_index]}; echo timeout ${cmd_list[$cmd_index]};" &
cmd_index=$((cmd_index+1))
running_count=`jobs | wc -l | awk '{print $1}'`
while [ $running_count -ge $LIMIT -a ]; do
running_count=`jobs | wc -l | awk '{print $1}'`
echo "running_count=$running_count,LIMIT=$LIMIT"
sleep 1
done
done
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment