加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – shell脚本:并行运行一批N个命令,等待全部完成,然后运行

发布时间:2020-12-15 18:34:54 所属栏目:安全 来源:网络整理
导读:任务:运行由3-5个命令组成的块(并行/后台). 示例块: dd if=/dev/urandom of=/mnt/1/x bs=1024 count=1024000000 dd if=/dev/urandom of=/mnt/2/x bs=1024 count=1024000000 dd if=/dev/urandom of=/mnt/3/x bs=1024 count=1024000000 完成后,下一个块应该
任务:运行由3-5个命令组成的块(并行/后台).
示例块:
dd if=/dev/urandom of=/mnt/1/x bs=1024 count=1024000000 &
dd if=/dev/urandom of=/mnt/2/x bs=1024 count=1024000000 &
dd if=/dev/urandom of=/mnt/3/x bs=1024 count=1024000000 &

完成后,下一个块应该运行.
我想,这可以通过锁定文件来完成:

task1.sh:

real_task1 real_param1 ; rm /var/lock/myscript/task1.lock

task2.sh:

real_task2 real_param1 ; rm /var/lock/myscript/task2.lock

taskgen.sh:

# loop
 # while directory isn't empty - wait...
 gen_tasks.pl # build task files from some queue
 for i in 1 2 3; do touch /var/lock/myscript/task$i.lock ; done
 ./task1.sh &
 ./task2.sh &
 ./task3.sh &
 # if task1.sh doesn't exits then exit,else loop waits for files to be deleted

检查目录是否为空的一些方法可以找到here,不确定使用哪个;

问题:有没有更好的方法来实现这个?

附:可能的状态报告方法:

command && report_good_state.sh taskid ;  report_state_done.sh taskid; rm /var/lock/myscript/taskN.lock
这正是070??00的设计目标,因此我强烈建议您使用它.特别是,看看它运行它 as a semaphore:
for i in {1..4}
do
  echo running $i
  sem -j3 df dd if=/dev/urandom of=/mnt/$i/x bs=1024 count=1024000000 ";" echo done
done
# sem --wait waits until all jobs are done.
sem --wait

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读