#!/bin/perl use strict; use warnings; my $pid = fork(); unless ($pid) { exec "nc -l 7777" or die; } local $SIG{ALRM} = sub { print "Command timed out!\n"; kill 9, $pid; }; alarm 2; waitpid $pid, 0; alarm 0;