欢迎来到代码驿站!

Shell

当前位置:首页 > 脚本语言 > Shell

Shell中实现“多线程”执行脚本文件完美解决方案

时间:2021-06-22 09:37:40|栏目:Shell|点击:

即比如我有100个可执行文件,互相间没有特别的先后执行关系,如CODE:

复制代码 代码如下:

job_1
job_2
job_2
.....
job_100

想用csh/bash来多线程调用执行。

比如一次开5个线程,那么job_1,2,3,4,5一起先开始,那么其中任何一个线程如果先执行完成,则继续执行下一个没有初执行过的文件,如job_6,7,8....,这样一直以所指定的线程数来执行所有100个文件。

我本来想用 "&" 来放入后台,可是这样我一次可以指定5放入后台,但是无法知道其中任何一个程序何时执行完毕,所以也无法继续执行下一个程序啊!

完美解决方案:

复制代码 代码如下:

-(dearvoid@LinuxEden:Forum)-(~/tmp)-
[$$=6718 $?=0] ; cat job_1
#!/bin/bash
n=$((RANDOM % 5 + 1))
echo "$0 sleeping for $n seconds ..."
sleep $n
echo "$0 exiting ..."
-(dearvoid@LinuxEden:Forum)-(~/tmp)-
[$$=6718 $?=0] ; for ((i = 2; i <= 10; ++i)); do cp job_1 job_$i; done
-(dearvoid@LinuxEden:Forum)-(~/tmp)-
[$$=6718 $?=0] ; cat jobs.sh
#!/bin/bash
nParellel=5
nJobs=10
sJobPattern='./job_%d'
aJobs=()
sNextJob=
for ((iNextJob = 1; iNextJob <= nJobs; )); do
    for ((iJob = 0; iJob < nParellel; ++iJob)); do
        if [ $iNextJob -gt $nJobs ]; then
            break;
        fi
        if [ ! "${aJobs[iJob]}" ] || ! kill -0 ${aJobs[iJob]} 2> /dev/null; then
            printf -v sNextJob "$sJobPattern" $((iNextJob++))
            echo "$sNextJob starting ..."
            $sNextJob &
            aJobs[iJob]=$!
        fi
    done
    sleep .1
done
wait
-(dearvoid@LinuxEden:Forum)-(~/tmp)-
[$$=6718 $?=0] ; ./jobs.sh
./job_1 starting ...
./job_1 sleeping for 3 seconds ...
./job_2 starting ...
./job_2 sleeping for 2 seconds ...
./job_3 starting ...
./job_3 sleeping for 5 seconds ...
./job_4 starting ...
./job_5 starting ...
./job_4 sleeping for 4 seconds ...
./job_5 sleeping for 2 seconds ...
./job_2 exiting ...
./job_6 starting ...
./job_6 sleeping for 2 seconds ...
./job_5 exiting ...
./job_7 starting ...
./job_7 sleeping for 1 seconds ...
./job_1 exiting ...
./job_8 starting ...
./job_8 sleeping for 3 seconds ...
./job_7 exiting ...
./job_9 starting ...
./job_9 sleeping for 5 seconds ...
./job_4 exiting ...
./job_6 exiting ...
./job_10 starting ...
./job_10 sleeping for 5 seconds ...
./job_3 exiting ...
./job_8 exiting ...
./job_9 exiting ...
./job_10 exiting ...
-(dearvoid@LinuxEden:Forum)-(~/tmp)-
[$$=6718 $?=0] ; bye

上一篇:shell日志颜色处理及清理系统日志的方法

栏    目:Shell

下一篇:Linux链接命令的实例详解

本文标题:Shell中实现“多线程”执行脚本文件完美解决方案

本文地址:http://www.codeinn.net/misctech/146402.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有