在脚本之间发送信号(bash)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25668841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:18:02  来源:igfitidea点击:

send signal between scripts (bash)

bashshellunixsignals

提问by Milite

I've a little problem, probably it's a stupid question, but I started learning bash about a week ago... I have 2 script, a.sh and b.sh. I need to make both running constantly. b.sh should waits for a signal from a.sh

我有一个小问题,可能这是一个愚蠢的问题,但我大约一周前开始学习 bash ......我有 2 个脚本,a.sh 和 b.sh。我需要让两者都持续运行。b.sh 应该等待来自 a.sh 的信号

(I'm trying to explain: a.sh and b.sh run --> a.sh sends a signal to b.sh -> b.sh traps signal, does something --> a.sh does something else and then sends another signal --> b.sh traps signal, does something --> etc.)

(我试图解释:a.sh 和 b.sh 运行 --> a.sh 向 b.sh 发送信号 -> b.sh 捕获信号,做某事 --> a.sh 做其他事,然后发送另一个信号 --> b.sh 捕获信号,做某事 --> 等)

This is what I've tried:

这是我尝试过的:

a.sh:

灰:

#!/bin/bash
./b.sh &;
bpid=$!;
# do something.....
while true
do
     #do something....
     if [ condition ]
     then
          kill -SIGUSR1 $bpid;
     fi
done

b.sh:

b.sh:

#!/bin/bash 
while true
do
     trap "echo I'm here;" SIGUSR1;
done

When I run a.sh I get no output from b.sh, even if I redirect the standard output to a file... However, when I run b.sh in background from my bash shell, it seems to answer to my SIGUSR1 (sent with the same command, directly from shell) (I'm getting the right output) What I'm missing?

当我运行 a.sh 时,即使我将标准输出重定向到一个文件,我也没有从 b.sh 得到任何输出......但是,当我从我的 bash shell 在后台运行 b.sh 时,它似乎回答了我的 SIGUSR1 (使用相同的命令直接从 shell 发送)(我得到了正确的输出)我错过了什么?

EDIT: this is a simple example that I'm trying to run:

编辑:这是我正在尝试运行的一个简单示例:

a.sh:

灰:

#!/bin/bash
./b.sh &
lastpid=$!;
if [ "" == "something" ]
then    
    kill -SIGUSR1 $lastpid;
fi

b.sh:

b.sh:

#!/bin/bash
trap "echo testlog 1>temp" SIGUSR1;
while true
do
    wait
done

I can't get the file "temp" when running a.sh. However if I execute ./b.sh &and then kill -SIGUSR1 PIDOFBmanually, everything working fine...

运行 a.sh 时我无法获取文件“temp”。但是,如果我执行./b.sh &然后kill -SIGUSR1 PIDOFB手动,一切正常...

采纳答案by constt

One of the possible solutions would be the next one (perhaps, it's dirty one, but it works):

可能的解决方案之一是下一个(也许它很脏,但它有效):

a.sh:

灰:

#!/bin/bash

BPIDFILE=b.pid

echo "a.sh: started"
echo "a.sh: starting b.sh.."

./b.sh &
sleep 1

BPID=`cat $BPIDFILE`

echo "a.sh: ok; b.sh pid: $BPID"

if [ "" == "something" ]; then
    kill -SIGUSR1 $BPID
fi

# cleaning up..
rm $BPIDFILE

echo "a.sh: quitting"

b.sh:

b.sh:

#!/bin/bash

BPIDFILE=b.pid

trap 'echo "got SIGUSR1" > b.log; echo "b.sh: quitting"; exit 0' SIGUSR1

echo "b.sh: started"

echo "b.sh: writing my PID to $BPIDFILE"
echo $$ > $BPIDFILE

while true; do
    sleep 3
done

The idea is to simply write down a PID value from within a b(background) script and read it from the a(main) script.

这个想法是简单地从b(后台)脚本中写下一个 PID 值,然后从a(主)脚本中读取它。