my $top_at_hour = 4; while ((localtime(time))[2] < $stop_at_hour) { # do some specific stuff here.. } exit;