#!/usr/bin/perl use strict; use warnings; use POSIX ":sys_wait_h"; $|++; print "Content-type:text/plain\n\nStarting script.....\n\n"; my $pid; if (!defined($pid = fork)) { die "Could not fork! $!"; } elsif ($pid) { # parent - clean up when child exits 1 while (waitpid($pid,WNOHANG) != -1); cleanup(); exit(0); } else { # child - does the processing print "Processing...\n\n"; sleep(5); print "Finished...\n\n"; exit(0); } sub cleanup { # do the clean up here }