等待脚本中的bash后台作业完成

前端之家收集整理的这篇文章主要介绍了等待脚本中的bash后台作业完成前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
为了最大化cpu使用率(我在EC2的Debian Lenny上运行东西)我有一个简单的脚本来并行启动作业:
#!/bin/bash

for i in apache-200901*.log; do echo "Processing $i ..."; do_something_important; done &
for i in apache-200902*.log; do echo "Processing $i ..."; do_something_important; done &
for i in apache-200903*.log; do echo "Processing $i ..."; do_something_important; done &
for i in apache-200904*.log; do echo "Processing $i ..."; do_something_important; done &
...

我对这个工作解决方案非常满意,但是我不知道如何编写更多的代码,只有在所有的循环都完成后才执行。

有没有办法控制这个?

有一个bash内置命令。
wait [n ...]
          Wait for each specified process and return its termination  sta‐
          tus.   Each  n  may be a process ID or a job specification; if a
          job spec is given,all processes  in  that  job’s  pipeline  are
          waited  for.  If n is not given,all currently active child pro‐
          cesses are waited for,and the return  status  is  zero.   If  n
          specifies  a  non-existent  process or job,the return status is
          127.  Otherwise,the return status is the  exit  status  of  the
          last process or job waited for.

猜你在找的Bash相关文章