gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 01:21:59 27 4
gpt4 key购买 nike

我启动了以下脚本,该脚本在前台的 bash shell(比方说 shell1)中运行,并从另一个 shell(shell2)发送 kill -SIGUSR1 pidof(scriptA)。什么都没发生。我究竟做错了什么 ?我尝试了其他信号(SIGQUIT 等),但结果是一样的。

测试陷阱.sh

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

在 shell1 中

./test_trap.sh

在 shell2 中

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

最佳答案

trap 直到 tail 完成才执行。但尾部永远不会结束。尝试:

tail -f /dev/null &
wait

trap 将在不等待 tail 完成的情况下执行,但如果您退出,tail 将继续运行。因此,您可能需要在陷阱中使用 kill $!

关于linux - 如何从另一个 shell 向 bash 脚本发送信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27435052/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com