简体   繁体   English

使用文件输入运行并行命令的Bash脚本

[英]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.

相关问题 有没有一种简单的方法可以使用小脚本自动读取文本文件并使用 bash 脚本和 awk 或其他 unix 命令进行绘图? - Is there an easy way to use a small script to automate reading of a text file and plotting using bash script with awk or other unix commands? 我可以在bash脚本中使用命令和文件名向grep发送字符串吗? - Can I send a string to grep with commands and file names in a bash script? 命令在终端中运行,但不在Bash脚本中运行 - Commands Working In Terminal, But Not In Bash Script 替换用户mid-Bash-script并继续运行命令(Mac OSX) - Substitute user mid-Bash-script & continue running commands (Mac OSX) 从bash脚本中的文件夹执行随机文件 - Execute random file from a folder in a bash script 排序从bash输入文件中读取的数字 - sorting numbers read in from input file in bash bash 脚本从数组中的列表中检查已安装的字体和 for 循环以执行命令 - bash script checking for installed fonts from list in an array and for loop to do commands 如何知道并行运行的所有后台命令的进程号 - How to know the process numbers of all the background commands running in parallel 在bash shell中使用输出文件中的数组 - Using array from an output file in bash shell 如何在bash脚本中匹配文本文件中的字符串值 - How to match string values from text file in bash script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM