http://www.perlmonks.org?node_id=833643

#!/usr/bin/perl use strict; use warnings; system("clear"); my ( $who_am_i, $countdown_pid, $checking_pid ); $countdown_pid = fork(); if ( $countdown_pid == 0 ) { $who_am_i = "countdown process"; } elsif ($countdown_pid) { $checking_pid = fork(); if ( $checking_pid == 0 ) { $who_am_i = "checking process"; } elsif ($checking_pid) { $who_am_i = "controlling process"; } else { die "Error forking: $!"; } } else { die "Error forking: $!"; } for ($who_am_i) { if ($who_am_i eq "countdown process") { sleep_count( 3, 'Verifying with Nagios that resin is down: ' ) +; } if ($who_am_i eq "checking process") { my $test_check = `/bin/ps -u dramaya`; my $counter = 0; while ( $test_check =~ /vi/ ) { if ( $counter == 310 ) { die "Could not verify if resin is down\n"; } $test_check = `/bin/ps -u dramaya`; sleep 10; $counter++; } } if ($who_am_i eq "controlling process") { # Wait on the "checking process" to complete. waitpid($checking_pid, 0); # Kill countdown timer kill(9, $countdown_pid); waitpid($countdown_pid, 0); } } print "$countdown_pid $checking_pid\n"; sub sleep_count { my $minutes = shift; my $string = shift; my $countdown = $minutes * 60; # in secs. $| = 1; my $beg_time = time; my $end_time = $beg_time + $countdown; for ( ; ; ) { my $time = time; last if ( $time >= $end_time ); printf( "\r[ %02d:%02d ] $string", ( $end_time - $time ) / (60) % 60, ( $end_time - $time ) % 60, ); sleep(1); } }