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