如何从另一个shell向bash脚本发送信号

时间:2021-07-09 20:50:06

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). Nothing happens. What am I doing wrong ? I tried other signals(SIGQUIT etc) but the result is same.

我启动以下脚本,我在前台运行bash shell(比如说shell1),从另一个shell(shell2)运行kill -SIGUSR1 pidof(scriptA)。什么都没发生。我究竟做错了什么 ?我尝试了其他信号(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}'

1 个解决方案

#1


4  

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 $! in the trap.

陷阱将在不等待尾部完成的情况下执行,但如果退出尾部将继续运行。所以你可能想要一个杀$!在陷阱里。

#1


4  

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 $! in the trap.

陷阱将在不等待尾部完成的情况下执行,但如果退出尾部将继续运行。所以你可能想要一个杀$!在陷阱里。