For a CGI process, you need to fork, have the parent return a standard response, and have the child close STDOUT. You're not doing anything in the right order in your code. I have some examples of using CGI with fork in my columns: read more there and get some stealable code.