File: wait-to-exec

package info (click to toggle)
percona-toolkit 3.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, trixie
  • size: 68,916 kB
  • sloc: perl: 241,287; sql: 22,868; sh: 19,746; javascript: 6,799; makefile: 353; awk: 38; python: 30; sed: 1
file content (40 lines) | stat: -rwxr-xr-x 870 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env perl

use strict;
use warnings FATAL => 'all';
use Time::HiRes qw(sleep time);

if ( !@ARGV || @ARGV < 2 ) {
   print STDERR "Usage: wait-to-exec WAIT EXEC [RUNTIME [INTERVAL]]\n",
                "When the WAIT script exits 0, execute the EXEC script.\n";
   exit 1;
}

my ($wait, $exec, $runtime, $interval) = @ARGV;
$runtime  ||= 1.0;
$interval ||= 0.1;

#warn "wait: $wait\n";
#warn "exec: $exec\n";

my $t_start = time;
while ( time - $t_start < $runtime ) {
   if ( exit_status($wait) ) {
      sleep $interval;
   }
   else {
      # exec does not return; this script becomes the exec process.
      exec($exec);
   }
}

warn "Never observed wait condition";
exit 1;

sub exit_status {
   my ($wait) = @_;
   system($wait);
   # Like grep, exit status 0 means "match found", so stop waiting.
   my $exit_status = $? >> 8;
   return $exit_status;
}