gpt4 book ai didi

linux - 并行运行多个 shell 函数实例

转载 作者:行者123 更新时间:2023-12-04 16:18:06 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How do you run multiple programs in parallel from a bash script?

(16 个回答)


6 个月前关闭。




我有一个 shell 脚本,如下所示。

#!/bin/bash

myfunc() {
#do something (call a rest service)
sleep 300
status=$(get status of the operation performed above)
while [ "$status" != "succeeded" ]; do
#do something (call a rest service)
sleep 300
status=$(get status of the operation performed above)
done
}

a=0

while [ $a -lt 1000 ]
do
a=`expr $a + 1`
myfunc
done
在最好的情况下,上面的脚本至少需要 5*1000=5000秒完成运行。
有没有办法让 myfunc调用 parallel 以便我们可以让 while 循环产生 myfunc 的多个正在运行的实例??
注意:不知何故,这个脚本应该等到 myfunc 的所有实例处决完成。

最佳答案

我认为您可以按如下方式更新您的脚本:

#!/bin/bash

myfunc() {
job_id=$1
echo "Started job ${job_id}..."

#do something (call a rest service)
sleep 300
status=$(get status of the operation performed above)
while [ "$status" != "succeeded" ]; do
#do something (call a rest service)
sleep 300
status=$(get status of the operation performed above)
done

echo "Terminated job ${job_id}."
}

a=0

while [ $a -lt 1000 ]
do
echo $a
a=(($a + 1))
myfunc $a &
done

wait
echo "Parallel execution terminated."

关于linux - 并行运行多个 shell 函数实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67906003/

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