bin/timed-process

filter: Statementstmt 86.3% 19 / 22 Branchbran 62.5% 5 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 3 / 4 Podpod n/a totaltotal 79.4% 27 / 34 CC 1 SCAR 2.2
CC
1
Cov
80%
usage
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.2
« blib/lib/Proc/Background.pm ↑ Summary
1 #!/usr/local/bin/perl -w
2  
3 =head1 NAME
4  
5 timed-process - Run background process for limited amount of time
6  
7 =head1 SYNOPSIS
8  
9 timed-process [-e exit_status] timeout command [<arg> [<arg> ...]]
10  
11 =head1 DESCRIPTION
12  
13 This script runs I<command> for a specified amount of time and if it
14 doesn't finish, it kills the process. If I<command> runs and exits
15 before the given timeout, B<timed-process> returns the exit value of
16 I<command>. If I<command> did not exit before I<timeout> seconds,
17 then B<timed-process> will kill the process and returns an exit value
18 of 255, unless the -e command line option is set, which instructs
19 B<timed-process> to return a different exit value. This allows the
20 user of B<timed-process> to determine if the process ended normally or
21 was killed.
22  
23 =cut
24  
25 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 ▶use Proc::Background 1.04 qw(timeout_system);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 6 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 6 $0 =~ s:.*/::;
30  
31 0 ▶sub usage {
Subroutine usage: not called CC=1 SCAR=6.9
32 0 print <<END;
33 usage: $0 [-e exit_status] timeout command [<arg> [<arg> ...]]
34  
35 This script runs command for a specified amount of time and if it
36 doesn't finish, it kills the process. If command runs and exits
37 before the given timeout, timed-process returns the exit value of
38 command. If command did not exit before timeout seconds, then
39 timed-process will kill the process and returns an exit value of 255,
40 unless the -e command line option is set, which instructs
41 timed-process to return a different exit value. This allows the user
42 of timed-process to determine if the process ended normally or was
43 killed.
44 END
45 0 exit 1;
46 }
47  
48 6 my $exit_status = 255;
49 6 Getopt::Long::Configure('require_order');
50 6 ▶GetOptions('exit-status=i', => \$exit_status) or
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("exit-status=i", \$exit_status) or usage() 6 0 6
51 usage;
52 6 ▶if ($exit_status < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_status < 0) 0 6 6
53 0 die "$0: exit status value `$exit_status' cannot be negative.\n";
54 }
55  
56 6 ▶@ARGV > 1 or usage;
Branch: 1 branch
BranchTrueFalseTotal
@ARGV > 1 or usage() 6 0 6
57  
58 6 my @result = timeout_system(@ARGV);
59  
60 3 ▶if ($result[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result[1]) { } 2 1 3
61 2 exit $exit_status;
62 } else {
63 1 exit $result[0] >> 8;
64 }
65  
« blib/lib/Proc/Background.pm ↑ Summary