gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 08:40:48 24 4
gpt4 key购买 nike

为了最大化 CPU 使用率(我在 EC2 的 Debian Lenny 上运行东西)我有一个简单的脚本来并行启 Action 业:

#!/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 - 等待脚本中的 bash 后台作业完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1131484/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com