我有一个 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
调用并行,以便我们可以让 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."
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)