my $pid = open2(*HIS_OUT, *HIS_IN, "/my/application" ) || die $!; my $now = time(); # you may want something more precise, whatever. while () { chomp; do_something_to($_); if ($now and time - $now > 4) { print HIS_IN "quit\n"; $now = undef; # don't keep doing this. } }