in reply to fork issue

It probably doesn't have to do anything with your observed problem, but do note that you are not checking whether the fork fails. If it fails, $pid is undefined, hence $pid == 0 is true and the *parent* (is you can talk of a parent if there is no child) sends mails and exits; the log will not be written.