Avoid race where child may exit before parent registers the PID. Also trap sigterm, since atexit is not always called if the process is terminated via sigterm