use Win32::Job; my $timeout = 30; # sec my $retry = 3; # times my $exe = "dir"; my $ok = 0; my $pid = undef; my $job = undef; # Try to execute command up to $retry times while ((! $ok) && ($retry > 0)) { # Create job $job = Win32::Job->new; # Create a new process and associate with the job $pid = $job->spawn("cmd", q{cmd /C $exe}, ); print "PID:$pid\n"; print "SYS:$^E\n"; # Run the job and kill it if more than $timeout sec $ok = $job->run($timeout); $retry = $retry - 1; }