我想知道是否有人可以帮忙解决这个问题?
我有一个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
更新了以下答案。我现在使用'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
已更新解决方案。我解决了这个问题。诀窍是使用非阻塞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
答案 0 :(得分:7)
这是一种不同的方法。您可以在GUI应用程序终止后立即执行任意命令,而不是使用SIGCHLD。
{
some_command args...
kill -USR1 $$
} &
function sigusr1() { ... }
trap sigusr1 SIGUSR1
答案 1 :(得分:1)
确定。我想我明白你需要什么。看看我的.xinitrc:
xrdb ~/.Xdefaults
source ~/.xinitrc.hw.settings
xcompmgr &
xscreensaver &
# after starting some arbitrary crap we want to start the main gui.
startfluxbox & PIDOFAPP=$! ## THIS IS THE IMPORTANT PART
setxkbmap genja
wmclockmon -bl &
sleep 1
wmctrl -s 3 && aterms sone &
sleep 1
wmctrl -s 0
wait $PIDOFAPP ## THIS IS THE SECOND PART OF THE IMPORTANT PART
xeyes -geometry 400x400+500+400 &
sleep 2
echo im out!
在将进程发送到后台后,您可以使用wait等待进程终止。只要应用程序正在运行,等待后的任何内容都不会被执行。关闭GUI后,可以使用此选项退出。
PS:我跑bash。
答案 2 :(得分:0)
我认为你需要这样做:
set -bm
或
set -o monitor notify
根据bash manual:
-b
Cause the status of terminated background jobs to be reported immediately, rather than before printing the next primary prompt.
答案 3 :(得分:0)
shell的主要工作是执行子进程,和 它需要为自己的目的捕获SIGCHLD。这在某种程度上限制了它将信号传递给脚本本身。
您是否可以检查孩子pid
并根据该发送警报。您可以在下面找到孩子pid
-
bash_pid=$$
while true
do
children=`ps -eo ppid | grep -w $bash_pid`
if [ -z "$children" ]; then
cleanup
alert
exit
fi
done