sta*_*fry 4 bash nonblocking child-process sigchld
我想知道是否有人可以帮助这个?
我有一个bash脚本.它启动一个子流程,这是另一个基于gui的应用程序.然后bash脚本进入交互模式,从用户获取输入.这种交互模式无限期地继续.我希望它在子进程中的gui-application退出时终止.
我看过SIGCHLD,但这似乎不是答案.这是我尝试过的,但是当编程结束时我没有收到信号.
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
Run Code Online (Sandbox Code Playgroud)
更新了以下答案.我现在使用'nosid'的建议.我现在有另一个相关的问题,即随后的交互式过程是一个基本的菜单驱动过程,它阻止等待来自用户的键输入.如果子进程结束,则直到收到输入后才处理USR1信号.有没有办法强制立即处理信号?
等待看起来像这样:
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
Run Code Online (Sandbox Code Playgroud)
更新了解决方案.我解决了这个问题.诀窍是使用非阻塞I/O进行读取.现在,根据'nosid'的答案和我的修改,我有我想要的.为了完整起见,这对我有用:
#!/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
Run Code Online (Sandbox Code Playgroud)
这是一种不同的方法.您可以在GUI应用程序终止后立即执行任意命令,而不是使用SIGCHLD.
{
some_command args...
kill -USR1 $$
} &
function sigusr1() { ... }
trap sigusr1 SIGUSR1
Run Code Online (Sandbox Code Playgroud)