通知后台作业在 bash 中完成运行
我正在运行一些耗时的可执行文件,这些可执行文件在循环中作为并行后台作业分配了不同的参数。这是一个玩具示例及其输出:
bash-3.2$ set -o notify
bash-3.2$ for (( i=0 ; i < 4 ; i+=2 )); do
> sleep ${i} &
> done
[1] 32394
[2] 32395
bash-3.2$ [1]- Done sleep ${i}
[2]+ Done sleep ${i}
请注意,当每个作业完成运行时,它会通知我。在完成消息中,将显示作业的命令,但以不扩展参数的方式显示,因此没有明显的方法通过查看消息来判断它正在使用什么参数值。我希望消息是什么样的
bash-3.2$ [1]- Done sleep 0
[2]+ Done sleep 2
可以这样做吗?
谢谢和问候!
I am running some time-consuming executables with different parameters assigned in a loop as background jobs in parallel. Here is a toy example and its output:
bash-3.2$ set -o notify
bash-3.2$ for (( i=0 ; i < 4 ; i+=2 )); do
> sleep ${i} &
> done
[1] 32394
[2] 32395
bash-3.2$ [1]- Done sleep ${i}
[2]+ Done sleep ${i}
Notice that when each job finishes running, it will notify me. In the finishing message the command of the job will be shown, but in the way that the parameter is not expanded, so there is not obvious way to tell what parameter value it is using by looking at the message. What I hope the message is like
bash-3.2$ [1]- Done sleep 0
[2]+ Done sleep 2
Is it possible to do so?
Thanks and regards!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不太漂亮:
输出如下:
函数可以稍微整理一下:
然后你可以这样做:
输出如下所示:
Not very pretty:
Output looks like:
A function could neaten it up a bit:
Then you can do:
and the output looks like:
如果你想等到最后一个完成..
每个 long_running_process (假设它是一个 shell 脚本)可以在终止之前立即打印出一条状态消息和它自己的 $1 。
If you're wanting to wait till the last one is done..
Each long_running_process (assuming its a shell script) can print out a status message and its own $1 immediately before termination.