I am currently working on a weird project 'Shellcord', which is a discord bot built only in bash and some builtins.
I am already able to get heartbeat interval. So to my question. My loop looks like this:
(while true
do
sleep $interval
echo '{"op":1,"d":null}' > ws0
done)&
Now, I am also able to shut down the bot. However, how can I then stop this heartbeat loop?
CodePudding user response:
Simplest way is something like:
(while true; do
sleep $interval
echo '{"op":1,"d":null}'
done)&
trap "kill $!" 0
If you have more to do in the exit cleanup, you'll need something more sophisticated. You may also be able to set up the trap to signal the process group before you start the loop. Something like:
pgid=$(ps -o pgid= $$| tr -d ' ')
trap 'kill -- -$pgid' 0