currently I'm writing a bash script like this:
foo(){
while true
do
sleep 10
done
}
bar(){
while true
do
sleep 20
done
}
foo &
bar &
wait
(I know there is no point in such a script, it's just about the structure)
Now I want to add signal handling with trap -- <doSomething> RTMIN+1
. This works at first. When the script receives the rtmin+1 signal it does doSomething but afterwards it exists (with the 163 exit code, which is the number of the signal being sent).
This is not the behavior I want. I want that after receiving the signal, the script continues to wait for the processes (in this case the two functions) to terminate (which of course will not happen in this case, but the script should wait).
I tried it with adding a ; wait
to the things that should be done when receiving the signal, but this does not help (or I'm doing something wrong).
Does anyone know how to achieve the desired behavior?
Thanks in advance and with best wishes.
EDIT: Maybe a more precise example helps:
clock(){
local prefix=C
local interval=1
while true
do
printf "${prefix} $(date '+%d.%m %H:%M:%S')\n"
sleep $interval
done
}
volume(){
prefix=V
volstat="$(amixer get Master 2>/dev/null)"
echo "$volstat" | grep "\[off\]" >/dev/null && icon="" #alternative: deaf: mute:
vol=$(echo "$volstat" | grep -o "\[[0-9]\+%\]" | sed "s/[^0-9]*//g;1q")
if [ -z "$icon" ] ; then
if [ "$vol" -gt "50" ]; then
icon=""
#elif [ "$vol" -gt "30" ]; then
# icon=""
else
icon=""
fi
fi
printf "${prefix}%s %3s%%\n" "$icon" "$vol"
}
clock &
volume &
trap -- "volume" RTMIN+2
wait
Now the RTMIN+2
signal should rerun the volume
function, but the clock
process should not be interrupted. (Up to now, the whole script (with all subprocesses) is terminated upon the receiving of the signal)