5 scripting shell-script parallelism
我有包含以下命令的文本文件
command1 file1_input; command2 file1_output
command1 file2_input; command2 file2_output
command1 file3_input; command2 file3_output
command1 file4_input; command2 file4_output
command1 file5_input; command2 file5_output
command1 file6_input; command2 file6_output
command1 file7_input; command2 file7_output
................
................
................
................
................
Run Code Online (Sandbox Code Playgroud)
我将此文件命名为“commands”,然后使用“ chmod a+x ” 授予它权限
我希望先运行命令 1,然后再运行命令 2。此外,我希望将其同时应用于所有文件(file1、file2、.... 等)。我怎样才能修改这个文件的内容来做到这一点?
我尝试了以下但没有奏效:
(
command1 file1_input; command2 file1_output
command1 file2_input; command2 file2_output
command1 file3_input; command2 file3_output
command1 file4_input; command2 file4_output
command1 file5_input; command2 file5_output
command1 file6_input; command2 file6_output
command1 file7_input; command2 file7_output
................
................
................
................
................
)&
Run Code Online (Sandbox Code Playgroud)
GNU 并行是这样做的:
$ parallel < /path/to/file/containing/commands
Run Code Online (Sandbox Code Playgroud)
让GNU并行管理过程VS在同一时间运行它们都在后台的优点是GNU平行可以限制并发作业的数量保持系统的内存和处理能力,例如,通过内--jobs,--load,--memfree,等。
如果您只是同时运行文件中的所有行,您的系统可能会耗尽 RAM 或 CPU 能力,从而使其变得极其缓慢。如果您的系统首先耗尽 RAM,然后耗尽交换空间,您的进程甚至可能开始崩溃。
使线条如下:
(command1 file1_input; command2 file1_output) &
(command1 file2_input; command2 file2_output) &
...
Run Code Online (Sandbox Code Playgroud)
每行将按顺序执行两个命令,但每行将作为并行后台作业进行分叉。
如果您希望仅在第一个命令成功完成时才执行第二个命令,请将分号更改为&&:
(command1 file1_input && command2 file1_output) &
(command1 file2_input && command2 file2_output) &
...
Run Code Online (Sandbox Code Playgroud)