通知后台作业在 bash 中完成运行

发布于 2024-08-04 05:48:13 字数 548 浏览 9 评论 0原文

我正在运行一些耗时的可执行文件,这些可执行文件在循环中作为并行后台作业分配了不同的参数。这是一个玩具示例及其输出:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮世清欢 2024-08-11 05:48:13

不太漂亮:

for (( i=0 ; i < 4 ; i+=2 )); do
    sleep ${i} && echo  -en "\nsleep ${i} "&
done

输出如下:

[1] 29311
[2]29313

sleep 0 [1]- 完成睡眠 ${i} && echo -en "\nsleep ${i} "
用户@主机:~$
sleep 2 [2]+ 完成 sleep ${i} && echo -en "\nsleep ${i} "

函数可以稍微整理一下:

myeval () {
    eval "$*" && echo -en "\n$* "
}

然后你可以这样做:

for (( i=0 ; i < 4 ; i+=2 )); do
    myeval "sleep ${i}"&
done

输出如下所示:

[1] 29635
[2]29636

睡眠 0 [1]- 完成我的“睡眠 ${i}”
用户@主机:~$
sleep 2 [2]+ 完成我的“sleep ${i}”

Not very pretty:

for (( i=0 ; i < 4 ; i+=2 )); do
    sleep ${i} && echo  -en "\nsleep ${i} "&
done

Output looks like:

[1] 29311
[2] 29313

sleep 0 [1]- Done sleep ${i} && echo -en "\nsleep ${i} "
user@host:~$
sleep 2 [2]+ Done sleep ${i} && echo -en "\nsleep ${i} "

A function could neaten it up a bit:

myeval () {
    eval "$*" && echo -en "\n$* "
}

Then you can do:

for (( i=0 ; i < 4 ; i+=2 )); do
    myeval "sleep ${i}"&
done

and the output looks like:

[1] 29635
[2] 29636

sleep 0 [1]- Done myeval "sleep ${i}"
user@host:~$
sleep 2 [2]+ Done myeval "sleep ${i}"

懒的傷心 2024-08-11 05:48:13

如果你想等到最后一个完成..

for ((i=0; i < 4; i+=2 )); do
    long_running_process $i &
done

wait

每个 long_running_process (假设它是一个 shell 脚本)可以在终止之前立即打印出一条状态消息和它自己的 $1 。

If you're wanting to wait till the last one is done..

for ((i=0; i < 4; i+=2 )); do
    long_running_process $i &
done

wait

Each long_running_process (assuming its a shell script) can print out a status message and its own $1 immediately before termination.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文