use POSIX qw( _exit ); if (!stat($png)) { my $child_code; local $SIG{CHLD} = sub { wait(); # Reap child $child_code = $?; }; defined( my $pid = fork() ) or die("Can't fork: $!\n"); if (!$pid) { # Child if (eval { create_thumbnail($pdf,$png); 1 }) { _exit(0); } else { # Especially deadly version of die() print STDERR $@; _exit($! || $?>>8 || 255); } } print "Waiting on $pid...\n"; sleep($timeout); # Interrupted by SIGCHLD since we have a handler if (defined($child_code)) { die("Child failure\n") if $child_code; } else { $SIG{CHLD} = 'DEFAULT'; kill KILL => $pid; wait(); # Reap child die("Child timeout\n"); } }