Skip to content

Instantly share code, notes, and snippets.

@ninehills
Created September 13, 2012 13:25
Show Gist options
  • Save ninehills/3714248 to your computer and use it in GitHub Desktop.
Save ninehills/3714248 to your computer and use it in GitHub Desktop.
并行执行文件中的命令的shell脚本
#!/bin/sh
# Usage: $0 <max_cmd> <cmdfile/stdin>
# max_cmd: 同时执行的命令数
# cmdfile/stdin: 标准输入或者命令文件,一条命令一行
set -u
max_cmd=$1
cmd_file=${2:--}
cur=0
line=1
for ((;;)) do
if [[ "$cmd_file" == "-" ]]; then
if ! read cmd; then
break
fi
else
if [[ $line -gt $(cat $cmd_file | wc -l) ]]; then
break
fi
cmd=$(sed -n ${line}p $cmd_file)
fi
line=$[line+1]
echo "$cmd"
sh -c "$cmd" &
pid[$cur]=$!
cur=$[cur+1]
while [[ $cur -ge $max_cmd ]]; do
pid_list=""
for i in `seq 0 1 $[max_cmd-1]`; do
if [[ -z "$pid_list" ]]; then
pid_list=${pid[i]}
else
pid_list=$pid_list,${pid[i]}
fi
done
cur=0
for i in $(ps -p $pid_list -o pid | sed 1d); do
pid[$cur]=$i
cur=$[cur+1]
done
if [[ $cur -ge $max_cmd ]]; then
sleep 1
fi
done
done
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment