简体   繁体   English

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

[英]How to send signal to a bash script from another shell

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). 我启动以下脚本,我在前台运行bash shell(比如说shell1),从另一个shell(shell2)运行kill -SIGUSR1 pidof(scriptA)。 Nothing happens. 什么都没发生。 What am I doing wrong ? 我究竟做错了什么 ? I tried other signals(SIGQUIT etc) but the result is same. 我尝试了其他信号(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}'

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 $! 所以你可能想要kill $! in the trap. 在陷阱里。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在bash shell脚本中添加另一个“for”循环? - How to add another “for” loop in bash shell script? bash脚本,从另一个shell执行shell并将结果分配给变量 - bash script, execute shell from another shell and assign results to a variable 如何从一个程序发送信号到另一个程序? - how to send signal from one program to another? 从bash shell中的另一个脚本调用脚本时,如何从调用脚本将前缀参数传递给被调用脚本? - When calling script from another script in bash shell, how do I pass prefixed arguments to the called script from calling script? 如何从文件中读取特定字符串并将整行保存在 Bash Shell 脚本中的另一个文件中 - How to read a specific string from a file and save the whole line in another file in Bash Shell script 如何正确标记从另一个bash脚本运行的bash脚本? - How to properly sigint a bash script that is run from another bash script? 如何从另一个Shell脚本运行一个Shell脚本,该脚本从CSV文件中读取第一个Shell脚本名称 - How to run a shell script from another shell script reading first shell script name from a CSV file 如何从另一个脚本启动一个 bash 脚本 - How to start one bash script from another 如何从bash shell脚本子程序返回数据? - How to return data from a bash shell script subroutine? 如何清除在 linux 中执行的 bash 脚本的 shell 的历史记录? - how to clear history of shell that a bash script executed from it in linux?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM