简体   繁体   English

如何使用 gnu_parallel 运行多个可执行和/或 bash 脚本?

[英]How to use gnu_parallel to run multiple executable and/or bash scripts?

I've been recently attempting to run my scripts in parallel in a more convenient way than to open a several instances of terminal and executing in scripts separately.我最近一直在尝试以一种比打开多个终端实例并分别在脚本中执行更方便的方式并行运行我的脚本。

I've been trying to learn how to use gnu_parallel for the past couple of days and I am still a bit clueless, and hoping if someone can provide a direct example.过去几天我一直在尝试学习如何使用 gnu_parallel ,但我仍然有点无能为力,希望有人能提供一个直接的例子。

Suppose I have a g++ compiled code called blah.exe and a bash script called blah.sh that will run alone perfectly fine, but I want to execute them in different directories.假设我有一个名为 blah.exe 的 g++ 编译代码和一个名为 blah.sh 的 bash 脚本,它们可以完美地单独运行,但我想在不同的目录中执行它们。

I've been reading https://www.gnu.org/software/parallel/man.html#EXAMPLE:-Working-as-xargs--n1.-Argument-appending我一直在阅读https://www.gnu.org/software/parallel/man.html#EXAMPLE:-Working-as-xargs--n1.-Argument-appending

and

https://www.biostars.org/p/182136/ https://www.biostars.org/p/182136/

but I am not totally clear about the syntax但我对语法并不完全清楚

To run these in series, I would do:要连续运行这些,我会这样做:

for i in 1 2 3 4
mv ./blah.exe directory$i
cd directory$i
./blah.exe all
cd ..
end

similarly相似地

for i in 1 2 3 4
mv ./blah.sh directory$i
cd directory$i
source ./blah.sh all
cd ..
end

I am trying to under stand how I would split this load to 4 logical-threads in one command using parallel.我试图理解如何在一个命令中使用并行将此负载拆分为 4 个逻辑线程。

Could someone provide an example for this?有人可以为此提供一个例子吗?

Thank you for your time.感谢您的时间。

Something like:就像是:

parallel --dry-run 'cd directory{}; ../blah.exe all; source ../blah.sh all' ::: {1..4}

No need to copy/move the executable, just run the same one.无需复制/移动可执行文件,只需运行相同的即可。

No need to cd .. afterwards, as it's a new process each time.无需cd ..之后,因为每次都是一个新进程。

Note this is not multi-threading, it is multi-processing.请注意,这不是多线程,而是多处理。


If you want to process discontiguous directory numbers, you can use:如果要处理不连续的目录号码,可以使用:

parallel ... ::: {1..4} 6 7 {11..14}

If you want to process all directories, you can use:如果要处理所有目录,可以使用:

printf "%s\0" */ | parallel -0 'cd {}; pwd' 

If you want to process all directories starting with FRED , you can use:如果要处理所有以FRED开头的目录,可以使用:

printf "%s\0" FRED*/ | parallel -0 'cd {}; pwd' 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM