如何从另一个shell发送信号到bash脚本 [英] How to send signal to a bash script from another shell

查看:301
本文介绍了如何从另一个shell发送信号到bash脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始下面的脚本,我在bash shell中运行(假设抽壳)的前景,并从另一个shell(shell2)我送杀-SIGUSR1的pidof(scriptA)。什么都没发生。我究竟做错了什么?我试过其他信号(SIGQUIT等),但结果是一样的。

test_trap.sh

 功能iAmDone {回声困信号;退出0}
陷阱iAmDone SIGUSR1
回声运行......
尾-f的/ dev / null的#什么也不做

在抽壳

  ./ test_trap.sh

在shell2

 杀-SIGUSR1的ps aux | grep的[T] est_trap | AWK'{打印$ 2}'


解决方案

该陷阱直到尾完成执行。但尾巴永远不会完成。尝试:

 尾-f的/ dev / null的&安培;
等待

陷阱将执行,而无需等待尾完成,但如果你退出尾巴会留下运行。所以,你可能会想一个 $杀!中的陷阱。

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}'

解决方案

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.

这篇关于如何从另一个shell发送信号到bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆