Signal Trapping in bash works only once in RHEL7 (but not in Ubuntu)

63 Views Asked by At

I have run into a strange (and probably platform specific issue) when attempting to do a trap-sleep loop.

Specifically, the following code:

saysomething() {
    trap saysomething 37
    echo "Hello there"
    while true; do
        sleep 1
    done
}

echo "Current pid: $$"
saysomething

prints "Hello there" everytime I send it a signal from another shell using kill -37 <pid> on Ubuntu 14.04.

However, on an RHEL 7 machine, the above prints Hello there the first time only. After that, the script keeps executing but fails to respond to any more signals.

What could be the reason for this difference?

1

There are 1 best solutions below

0
On

Not really an answer, but this is the workaround I ended up using the following as a workaround:

#!/usr/bin/env bash

flag=false

saysomething() {
    flag=true   
    trap reset 37
    echo "Hello there"
    while [[ $flag == true ]]; do
        sleep 1
    done
    saysomething
}

reset() {
    flag=false
}

echo "Current pid: $$"

saysomething

I still can't explain the reason for this difference in behaviour though.