我如何收到通知,bash脚本当一个特定的子进程结束? [英] How do I receive notification in a bash script when a specific child process terminates?

查看:147
本文介绍了我如何收到通知,bash脚本当一个特定的子进程结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否有人能帮助您?

我有一个bash脚本。它启动子方法,该方法是另一种基于GUI的应用程序。在bash脚本然后进入交互模式从用户获得的输入。这种互动模式无限期地继续。我想它终止当GUI应用程序的子进程退出。

我已经看过SIGCHLD,但这似乎并没有得到答案。这是我做过尝试,但在PROG结束我没有得到一个信号。

 设置-o显示器$ {} PROG与&
prog_pid = $!功能check_pid {
    杀-0 $ 1 2>的/ dev / null的
}功能清理{
    ###做清理的东西在这里
    出口
}
功能SIGCHLD {
    check_pid $ prog_pid
    [[$? == 1]]&放大器;&安培;清理
}陷阱SIGCHLD SIGCHLD

更新如下回答。我现在有这个利用nosid的建议工作。我有另外,相关的,现在的问题是该遵循的交互过程是块等待来自用户的键输入一个基本菜单驱动的过程。如果子进程结束USR1信号没有被处理接收到的输入之后,直到。有什么办法来强制信号要立即处理?

等待的样子看起来是这样的:

  stty的原料#设置tty驱动原始模式
最大= $ 1号最大有效选择
选择= $(表达式$ MAX + 1)#无效的选择
而[[$选择-gt $ MAX]];做
    选择=`DD如果=的/ dev / tty的BS = 1数= 1 2 - ;为/ dev / null`
DONE
stty的理智#恢复TTY

更新与解决方案。我已经解决了这一点。诀窍是使用非阻塞I / O进行读取。现在,在nosid'和我的修改答案,我有我想要的东西。为了完整起见,这里是我的什么作品:

 #!/斌/ bash的-bm
{
$ {1}
杀-USR1 $$
}&安培;功能清理{
    #清理的东西
    出口
}陷阱清理SIGUSR1而真实的;做
   stty的原料#设置tty驱动原始模式
   最大= 9#最大有效选择
   而[[$选择-gt $最大|| -z $选择]];做
       选择=`DD IFLAG = NONBLOCK如果=的/ dev / tty的BS = 1数= 1 2 - ;为/ dev / null`
   DONE
   stty的理智#恢复TTY   #工艺的选择DONE


解决方案

这是一个不同的方法。而不是使用SIGCHLD的,可以作为GUI应用程序终止立即执行任意命令。

  {
    some_command ARGS ...
    杀-USR1 $$
}&安培;功能SIGUSR1(){...}陷阱SIGUSR1 SIGUSR1

I wonder if anyone can help with this?

I have a bash script. It starts a sub-process which is another gui-based application. The bash script then goes into an interactive mode getting input from the user. This interactive mode continues indefinately. I would like it to terminate when the gui-application in the sub-process exits.

I have looked at SIGCHLD but this doesn't seem to be the answer. Here's what I've tried but I don't get a signal when the prog ends.

set -o monitor

"${prog}" &
prog_pid=$!

function check_pid {
    kill -0 $1 2> /dev/null
}

function cleanup {
    ### does cleanup stuff here
    exit
}


function sigchld {
    check_pid $prog_pid
    [[ $? == 1 ]] && cleanup
}

trap sigchld SIGCHLD

Updated following answers. I now have this working using the suggestion from 'nosid'. I have another, related, issue now which is that the interactive process that follows is a basic menu driven process that blocks waiting for key input from the user. If the child process ends the USR1 signal is not handled until after input is received. Is there any way to force the signal to be handled immediately?

The wait look looks like this:

stty raw                 # set the tty driver to raw mode 
max=$1                   # maximum valid choice
choice=$(expr $max + 1)  # invalid choice
while [[ $choice -gt $max ]]; do
    choice=`dd if=/dev/tty bs=1 count=1 2>/dev/null`
done
stty sane                # restore tty

Updated with solution. I have solved this. The trick was to use nonblocking I/O for the read. Now, with the answer from 'nosid' and my modifications, I have exactly what I want. For completeness, here is what works for me:

#!/bin/bash -bm
{
"${1}"
kill -USR1 $$
} &

function cleanup {
    # cleanup stuff
    exit
}

trap cleanup SIGUSR1

while true ; do
   stty raw                 # set the tty driver to raw mode 
   max=9                    # maximum valid choice
   while [[ $choice -gt $max || -z $choice ]]; do
       choice=`dd iflag=nonblock if=/dev/tty bs=1 count=1 2>/dev/null`
   done
   stty sane                # restore tty

   # process choice       

done

解决方案

Here is a different approach. Instead of using SIGCHLD, you can execute an arbitrary command as soon as the GUI application terminates.

{
    some_command args...
    kill -USR1 $$
} &

function sigusr1() { ... }

trap sigusr1 SIGUSR1

这篇关于我如何收到通知,bash脚本当一个特定的子进程结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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