send signal between scripts (bash)

岁酱吖の 提交于 2019-12-04 19:07:46

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 [ "$1" == "something" ]; then
    kill -SIGUSR1 $BPID
fi

# cleaning up..
rm $BPIDFILE

echo "a.sh: quitting"

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!