我有一系列要在其上运行相同命令的服务器。每个命令要花费几个小时,并且(即使我使用nohup运行命令并将它们设置为在后台运行),我也必须等待每个命令完成,然后再开始下一个命令。我大致是这样设置的:
在主机上:
for i in {1..9}; do ssh RemoteMachine${i} ./RunJobs.sh; done
每台远程计算机上的RunJobs.sh是:
source ~/.bash_profile
cd AriadneMatching
for file in FileDirectory/Input_*;
do
nohup ./Executable ${file} &
done
exit
有谁知道让我不必等下一项工作就可以开始下一项工作的方式吗?或替代地,做这件事的一种更好的方法,我觉得我做的是次优的。干杯,杰克
# pssh -P --par 2 --hosts RemoteMachines /opt/RunJobs.sh
或者:
# pssh -i --par 2 --hosts RemoteMachines /opt/RunJobs.sh
参数说明:
-P
--print
Display output as it arrives. This option is of limited usefulness
because output from different hosts are interleaved.
-i
--inline
Display standard output and standard error as each host completes.
-p parallelism
--par parallelism
Use the given number as the maximum number of concurrent connections.
-h host_file
--hosts host_file
Read hosts from the given host_file.
# ansible --forks 2 -i RemoteMachines '*' -m command -a /opt/RunJobs.sh
参数说明:
-f NUM, --forks=NUM
Level of parallelism. NUM is specified as an integer, the default is 5.
-i PATH, --inventory=PATH
The PATH to the inventory hosts file, which defaults to /etc/ansible/hosts.
-m NAME, --module-name=NAME
Execute the module called NAME.
-a 'ARGUMENTS', --args='ARGUMENTS'
The ARGUMENTS to pass to the module.
该命令模块执行命令名称后面的空格分隔的参数列表。给定的命令将在所有选定的节点上执行。它不会通过外壳进行处理,因此$ HOME之类的变量以及“ <”,“>,” |“和”&“之类的操作将不起作用。
您可以在Ad-Hoc命令简介中阅读更多内容。
NB ansible不会切换到主机的下一组,直到所有的当前主机(“叉”)完成,因此它的并行度低比PSSH(有可能增加它的一种方式,但我不知道) 。
在这两种情况下,RemoteMachines文件都看起来像这样:
[email protected]
[email protected]
[email protected]
[email protected]
将RunJobs.sh重写为如下所示:
find FileDirectory -name 'Input_*' -print0 | xargs -0 -P 2 -n 1 ./Executable
说明:
-0, --null
Input items are terminated by a null character instead of by
whitespace, and the quotes and backslash are not special (every
character is taken literally). Disables the end of file string,
which is treated like any other argument. Useful when input items
might contain white space, quote marks, or backslashes. The GNU find
-print0 option produces input suitable for this mode.
-P max-procs, --max-procs=max-procs
Run up to max-procs processes at a time; the default is 1. If
max-procs is 0, xargs will run as many processes as possible at a
time. Use the -n option or the -L option with -P; otherwise chances
are that only one exec will be done.
-n max-args, --max-args=max-args
Use at most max-args arguments per command line. Fewer than
max-args arguments will be used if the size (see the -s option) is
exceeded, unless the -x option is given, in which case xargs will
exit.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句