save
[mkws-moved-to-github.git] / test / bin / bomb.pl
1 #!/usr/local/bin/perl
2 # Copyright (c) 2014 IndexData ApS. http://indexdata.com
3 #
4 # bomb.pl - wrapper to stop a process after N seconds
5 #
6
7 use Getopt::Long;
8
9 use strict;
10 use warnings;
11
12 my $debug = 0;
13 my $help;
14 my $timeout = 100;
15
16 binmode \*STDOUT, ":utf8";
17 binmode \*STDERR, ":utf8";
18
19 # timeout handler
20 sub set_alarm {
21     my $time = shift;
22     my $message = shift || "";
23
24     $time = 100 if !defined $time;
25
26     $SIG{ALRM} = sub {
27
28         warn "Time out alarm $time\n";
29
30         # sends a hang-up signal to all processes in the current process group
31         # and kill running java processes
32         local $SIG{HUP} = "IGNORE";
33         kill 1, -$$;
34
35         local $SIG{TERM} = "IGNORE";
36         kill 15, -$$;
37         kill 15, -$$;
38
39         warn "Send a hang-up to all childs.\n";
40
41         #exit 1;
42     };
43
44     warn "set alarm time to: $time seconds $message\n" if $debug >= 1;
45     alarm($time);
46 }
47
48 sub usage () {
49     <<EOF;
50 usage: $0 [ options ] command args ....
51
52 --debug=0..3    debug option, default: $debug
53 --timeout=1..N  timeout in seconds, default: $timeout
54 EOF
55 }
56
57 GetOptions(
58     "help"      => \$help,
59     "debug=i"   => \$debug,
60     "timeout=i" => \$timeout,
61 ) or die usage;
62
63 my @system = @ARGV;
64
65 die usage if $help;
66 die usage if !@system;
67
68 set_alarm( $timeout, join( " ", @system ) );
69
70 system(@system) == 0
71   or die "system @system failed: $?";
72
73 exit(0);