#!/usr/bin/perl -w use strict; use warnings; # ----- // FORKING // ------------------------- { # fork-o-rama! my($pid) = fork; ++$|; die(qq[I ($$) Can't fork $!\012\012]) if (!defined($pid)); if (!$pid) { # AM CHILD PROC # handle whatever task you want, then exit... exit; # ALWAYS DO THIS! } else { # AM PARENT PROC # wait on the child proccess waitpid( $pid, 0 ); } } # ----- // DONE FORKING // -------------------- =PROC EVENT HANDLERS. Use this code if you want to see track proccess creation and termination. BEGIN { print(qq[\n\nproc $$ beginning now.\n\n]) } END { print(qq[\n\nproc $$ exiting now.\n\n]) } =cut =PROC SIGNAL HANDLERS Use this code if you want to track system sinals while forking. BEGIN { foreach (keys(%SIG)) { my($key) = $_; $SIG{$key} = sub { $key = '??' unless (defined($key) and length($key)); warn(scalar(localtime), qq[ -- Got SIG $key\012]) } } undef($!) } =cut =INTERNAL LOGGING. redirects STDERR to a log Use this code if you want STDERR directed to a private log BEGIN { local(*EL); open(EL,'>>./error.log') and open(STDERR,'>&EL') } =cut =THERE'S MORE THAN ONE WAY TO waitpid() NORMAL WAY TO USE waitpid() waitpid( $pid, 0 ) and print(qq[ done waiting on $pid.]); NO HANG WAY TO USE waitpid() use POSIX qw( :sys_wait_h ); waitpid( $pid, &POSIX::WNOHANG() ) and print(qq[ done waiting on $pid.]); =cut