use IO::Handle; sub daemonize { my $log = shift; $log->autoflush( 1 ); # forking code goes here if ($pid > 0) { print {$log} "Forking Success. Child PID: $pid \n"; exit(); } print {$log} "Child Process Executing... \n"; setsid(); # ... }