脚本在收到 SIGUSR1/SIGUSR2 后终止

问题描述 投票:0回答:1

我有一个简单的 bash 脚本,可以像这样监听

SIGUSR{1,2}

#!/usr/bin/env bash

trigger() {
  echo "receiving signal"
}

echo $$
trap trigger USR1 USR2
sleep inf

所以我运行了脚本然后运行:

kill -USR1 -$PID # where $PID is the PID printed by the script

receiving signal
被打印出来,但随后脚本就消失了。

有没有办法防止这种情况,以便脚本可以继续监听

SIGUSR{1,2}
信号?

bash shell
1个回答
0
投票

这(未经测试)可能就是您想要做的:

$ cat tst.sh
#!/usr/bin/env bash

trigger() {
  echo 'receiving signal'
}

echo "$$"
trap 'trigger' USR1 USR2

while true; do
    sleep inf
done
© www.soinside.com 2019 - 2024. All rights reserved.