[英]How to send signal to a bash script from another shell
I start the following script which I run in a bash shell(let's say shell1) in foreground and from another shell(shell2) I send the kill -SIGUSR1 pidof(scriptA). 我启动以下脚本,我在前台运行bash shell(比如说shell1),从另一个shell(shell2)运行kill -SIGUSR1 pidof(scriptA)。 Nothing happens.
什么都没发生。 What am I doing wrong ?
我究竟做错了什么 ? I tried other signals(SIGQUIT etc) but the result is same.
我尝试了其他信号(SIGQUIT等)但结果是一样的。
test_trap.sh test_trap.sh
function iAmDone { echo "Trapped Signal"; exit 0 }
trap iAmDone SIGUSR1
echo "Running... "
tail -f /dev/null # Do nothing
In shell1 在shell1中
./test_trap.sh
In shell2 在shell2中
kill -SIGUSR1 ps aux | grep [t]est_trap | awk '{print $2}'
The trap is not executed until tail finishes. 在尾部完成之前不会执行陷阱。 But tail never finishes.
但尾巴永远不会完成。 Try:
尝试:
tail -f /dev/null &
wait
The trap will execute without waiting for tail to complete, but if you exit the tail will be left running. 陷阱将在不等待尾部完成的情况下执行,但如果退出尾部将继续运行。 So you'll probably want a
kill $!
所以你可能想要
kill $!
in the trap. 在陷阱里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.