如何脚本的后台作业完成时得到通知? [英] How to be notified when a script's background job completes?

查看:144
本文介绍了如何脚本的后台作业完成时得到通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是非常相似,这<一个href=\"http://stackoverflow.com/questions/1369522/notify-upon-background-jobs-finishing-running-in-bash\">one除了我的后台进程是从脚本启动。我可能是做错了什么,但是当我试试这个简单的例子:

 #!/斌/庆典
集-MB#使作业控制和通知
睡5和

我从来没有收到通知睡眠背景命令完成时。但是,如果我直接在终端执行相同的,

  $集-MB
$睡眠5和
$
[1] +完成睡眠5

我看到我期望的输出。

我使用的cygwin的bash。我猜它可能是与那里的输出直接,但尝试各种输出重定向,我没有得到任何进展。

编辑:所以我对为什么(感谢jkramer)的更多信息,但仍在寻找如何。我如何获得推通知后台进程从剧本开始就终止?保存PID到文件和投票是不是我要找的。

解决方案

如果你运行它作为源文件。那么它可以通知。例如。

 猫foo.sh#!/斌/庆典
集-MB#使作业控制和通知
睡5和。 foo.sh
[1] +完成睡眠5

My question is very similar to this one except that my background process was started from a script. I could be doing something wrong but when I try this simple example:

#!/bin/bash
set -mb  # enable job control and notification
sleep 5  &

I never receive notification when the sleep background command finishes. However, if I execute the same directly in the terminal,

$ set -mb  
$ sleep 5  &
$
[1]+  Done                    sleep 5

I see the output that I expect.

I'm using bash on cygwin. I'm guessing that it might have something to do with where the output is directed, but trying various output redirection, I'm not getting any closer.

EDIT: So I have more information about the why (thx to jkramer), but still looking for the how. How do I get "push" notification that a background process started from a script has terminated? Saving a PID to a file and polling is not what I'm looking for.

解决方案

if you run it as source file . then it can notify. e.g.

cat foo.sh

#!/bin/bash
set -mb  # enable job control and notification
sleep 5  &

. foo.sh
[1]+  Done                    sleep 5

这篇关于如何脚本的后台作业完成时得到通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆