my $some_host = ... my $pid = open TRACE, "traceroute $some_host |"; while () { if (/some string/) { close TRACE; kill 15, $pid; last; } # other processing here } # rest of script here }