Thanks for the tips. I have updated the code to use flock - a much smarter method. I was thinking that an END block may be a good way to debug how this script is exiting. However, since it's not a function call I am not sure what useful information I can print out in the END block.
Will an END block run if the process receives a SIGKILL, SIGSTOP, or SIGQUIT? If so, is there any way to log the recipt of these signals since I can't create a handler for the first two. Thanks