in reply to Keep running script but ending it to the browser

Why not fork a separate process from your script to handle it? Something like this (untested):

END { my $pid; if ($pid = fork){ exit; }elsif (defined $pid){ close STDOUT; sleep 5; unlink ($tempfile); }else{ # if fork fails: sleep 5; unlink ($tempfile); } }