wait in loop to avoid wait returning due to EINTR
removed printf debug statements
changed rc from sh to bash