How to send signal to a bash script from another s

2019-06-24 04:02发布

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.

test_trap.sh

function iAmDone { echo "Trapped Signal"; exit 0 } 
trap iAmDone SIGUSR1 
echo "Running... " 
tail -f /dev/null # Do nothing

In shell1

./test_trap.sh

In shell2

kill -SIGUSR1 ps aux | grep [t]est_trap | awk '{print $2}'

标签: linux bash shell
1条回答
等我变得足够好
2楼-- · 2019-06-24 04:31

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.

查看更多
登录 后发表回答