You need to call daemonize before you start running other stuff. You should split your GetOptions subroutine into two. One that just gets the options and does any inilization stuff (e.g. variable assignment) that's needed. Then call daemonize. And then start doing your system calls.