linux - 并行运行 shell 函数的多个实例,而不将输出与其他作业混合
问题描述
我有一个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
echo "Starting myfunc with process : $a"
a=`expr $a + 1`
myfunc
echo "Finished myfunc with process : $a"
done
在最好的情况下,上述脚本至少需要5*1000=5000
几秒钟才能完成运行。
有没有办法使myfunc
调用并行,以便我们可以让 while 循环产生多个正在运行的myfunc
??
除此之外,我希望满足以下条件。
- 该脚本应该等到所有
myfunc
执行实例都完成。 - 上面的输出应该如下所示(它应该保留回显顺序)。
Starting myfunc with process : x
Finished myfunc with process : x
Starting myfunc with process : y
Finished myfunc with process : y
Starting myfunc with process : k
Finished myfunc with process : k
它不应该像下面那样。
Starting myfunc with process : x
Starting myfunc with process : k
Finished myfunc with process : k
Starting myfunc with process : z
Finished myfunc with process : x
Finished myfunc with process : z
解决方案
推荐阅读
- javascript - 多个 AddEventListener 表单提交
- spring-boot - 在百里香弹簧靴中,动态旋转木马滑块不起作用
- oracle - Oracle 更新语句与 Join
- git - 尝试克隆 TensorFlow 存储库时 git clone 不起作用
- amazon-web-services - AWS CloudFormation:新创建的 EC2 密钥对出现“密钥对 ___ 不存在”错误
- python - Where to initialize SQLAlchemy instance with Flask-restplus
- c# - 使用 win32 api 获取 Treeview(SysTreeView32) 项目文本
- ios - 如何让我的文本字段在 iOS 中消失?
- php - 语句中的打印函数始终返回 true
- php - Httpful 帖子和 URL 参数