简体   繁体   English

如何在bash shell中同时运行多个作业

[英]how to run multiple jobs simultaneously in bash shell

I would like to run the same bash script multiple times on multiple ID's. 我想在多个ID上多次运行相同的bash脚本。

Here is the script that i was talking about: 这是我正在谈论的脚本:

#!/bin/bash
export PATH=/diag/software/bin:$PATH
cd /diag/cloud/udevisetty/RIL_stuff/Block_01
fq_dir="/diag/cloud/udevisetty/RIL_stuff/Block_01"
map_dir="/diag/cloud/udevisetty/RIL_stuff/mapping/"

for RIL in $(cat /diag/cloud/udevisetty/RIL_stuff/Block_01/test.txt); do
      for lane in 01; do
            perl /diag/cloud/udevisetty/RIL_stuff/Block_01/bwa_tophat_final.pl \
            --threads 12 \
            --out $map_dir/$RIL.$lane \
            --bwa_n 0.04 \
            --bwa_db /diag/home/udevisetty/db/Brapa_sequence_v1.5.fa \
            --fq $fq_dir/$RIL/*fq \
            --fq_id $RIL.$lane \
            --bowtie_db /diag/home/udevisetty/db/Brapa_sequence_v1.5.fa \
            --ref_fa /diag/home/udevisetty/db/Brapa_sequence_v1.5.fa \
            --ref_id Brapa_v1.5 \
            > $map_dir/$RIL.$lane.bwa_tophat.log \
            2> $map_dir/$RIL.$lane.bwa_tophat.err
done;
done

My test file contains the following ID's 我的测试文件包含以下ID

RIL_251
RIL_21
RIL_211
RIL_136
RIL_66
RIL_155

But right now all it is doing is going through each ID in test.txt file (since it is a 'for' loop) and then executing the script one at a time. 但是现在,它要做的只是遍历test.txt文件中的每个ID(因为它是一个“ for”循环),然后一次执行一个脚本。 However i would like to run all of the IDs simultaneously on 12 threads each. 但是我想同时在12个线程上同时运行所有ID。 How can i modify my script to do that? 我该如何修改我的脚本来做到这一点?

A contrived exampled illustrating how to do this with 'xargs': 一个人为的示例,说明了如何使用“ xargs”执行此操作:

root# cat id.txt | xargs -n 1 -P 3 ./script.sh
id2
id1
id4
id3
id5
id7
id6

Contents of id.txt, notice it's ordered. id.txt的内容,请注意已订购。

root# cat id.txt
id1
id2
id3
id4
id5
id6
id7

Script contents: 脚本内容:

root# cat script.sh
#!/bin/bash
echo $1

You can also use GNU Parallel if available here 您也可以在此处使用GNU Parallel

To summarize: 总结一下:

while read -u 3 -r line; do
  process "$line" &
done 3< input.txt

This follows the BashFAQ #001 best-practice approach for reading lines from the file (using FD 3 to avoid reassigning the descriptor for stdin), and uses the & operator to background each process. 这遵循BashFAQ#001最佳实践方法从文件中读取行(使用FD 3以避免为stdin重新分配描述符),并使用&运算符为每个进程设置背景。

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

相关问题 如何使用单独的线程运行多个 shell 作业并在同时执行每个作业后等待每个作业完成? - How to run multiple shell jobs using separate threads and wait for each to finish after executing each simultaneously? 如何在多个GPU(2)上同时运行OpenCL? - How to run OpenCL on multiple GPUs (2) simultaneously? 如何同时多次运行同一个函数? - How to run the same function multiple times simultaneously? 同时执行多个备份作业:理论与实践 - Multiple Backup Jobs Simultaneously: Theory vs Practice 如何同时/同步在特定的多个内核/处理器上运行多个任务 - How to run multiple tasks on specific multiple cores/processors simultaneously/synchronously 重击,同时运行多个命令,等待N完成再生成更多 - Bash, run multiple commands simultaneously, wait for N to finish before spawning more 如何在Python中同时运行多个tkinter窗口? - How do I run multiple tkinter windows simultaneously in Python? 如何同时运行多个SOAP调用 - How do I run multiple SOAP calls simultaneously 如何使一个函数同时运行多次? - How do I make a function run multiple times simultaneously? Cron作业是否同时在Google App Engine中运行? - Do cron jobs run simultaneously in google app engine?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM