[英]Bash script for running parallel commands using input from a file
I am trying to make a shell script which reads a configuration file and executes commands for each line in parallel. 我正在尝试制作一个Shell脚本,该脚本读取配置文件并并行执行每一行的命令。
For example, I have IPs.cfg, which can contain a variable amount of IPs. 例如,我有IPs.cfg,其中可以包含可变数量的IP。 It could be one or several
可能是一个或几个
IPs.cfg IPs.cfg
145.x.x.x
176.x.x.x
192.x.x.x
I want to read the file and then execute a command for each line at the same time... for instance : 我想读取文件,然后同时对每行执行一个命令...例如:
scp test.iso root@$IP1:/tmp &
scp test.iso root@$IP2:/tmp &
scp test.iso root@$IP3:/tmp &
wait
The way I'm thinking this is that I store the IPs into an array 我认为这是将IP存储到阵列中的方式
IFS=$'\n' read -d '' -r -a array < IPs.cfg
Then I extract the number of lines from the file and decrease it by 1 since the array starts at 0. 然后我从文件中提取行数,并将其减少1,因为数组从0开始。
NUMLINES=`cat IPs.cfg | wc -l`
NUMLINES=$((NUMLINES-1))
Now I want to execute the commands all at the same time. 现在,我想同时执行所有命令。 It's a variable number of parameters so I can't just manually use
scp test.iso root@${array[0]}:/tmp & scp test.iso root@${array[1]}:/tmp & so on
. 这是可变数量的参数,因此我不能仅手动使用
scp test.iso root@${array[0]}:/tmp & scp test.iso root@${array[1]}:/tmp & so on
。 I could use a while loop, but that would mean doing the commands one at a time. 我可以使用while循环,但这意味着一次执行一个命令。 I'm also thinking about using recursion, but I have never done that in a bash script.
我也在考虑使用递归,但是我从未在bash脚本中做到这一点。
It might be a silly question, but what are my options here? 这可能是一个愚蠢的问题,但是我在这里有什么选择?
您可以利用GNU并行
The loop should look like this: 循环应如下所示:
while read -r ip ; do
scp test.iso "root@$ip:/tmp" &
done < IPs.conf
wait
with this trick, you can control the number of simultaneous processes running at the same: 使用此技巧,您可以控制同时运行的并发进程数:
cat IPs.conf | xargs -n1 -I{} -P10 scp test.iso "root@{}:/tmp"
Check the -p10
which means to use 10 processes at the same time. 选中
-p10
表示同时使用10个进程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.