File Coverage

blib/lib/App/nodie.pm
Criterion Covered Total %
statement 31 80 38.7
branch 0 40 0.0
condition 0 9 0.0
subroutine 10 13 76.9
pod 0 3 0.0
total 41 145 28.2


line stmt bran cond sub pod time code
1             package App::nodie;
2             =head1 NAME
3              
4             App::nodie - runs command again when its dead
5              
6             =head1 VERSION
7              
8             version 1.03
9              
10             =head1 SYNOPSIS
11              
12             #!/bin/sh
13             perl -MApp::nodie -erun -- command arg1 arg2 ...
14              
15             =head1 DESCRIPTION
16              
17             App::nodie runs command again when its dead.
18              
19             See also: L
20              
21             =cut
22 1     1   13599 use strict;
  1         2  
  1         25  
23 1     1   5 use warnings;
  1         2  
  1         19  
24 1     1   11 use v5.10.1;
  1         6  
25 1     1   4 use feature qw(switch);
  1         2  
  1         99  
26 1     1   522 no if ($] >= 5.018), 'warnings' => 'experimental';
  1         10  
  1         5  
27 1     1   540 use FindBin;
  1         774  
  1         38  
28 1     1   6 use File::Basename;
  1         2  
  1         40  
29 1     1   5 use Scalar::Util qw(looks_like_number);
  1         1  
  1         55  
30 1     1   399 use Lazy::Utils;
  1         38732  
  1         134  
31              
32              
33             BEGIN {
34 1     1   7 require Exporter;
35 1         3 our $VERSION = '1.03';
36 1         8 our @ISA = qw(Exporter);
37 1         2 our @EXPORT = qw(main run);
38 1         539 our @EXPORT_OK = qw();
39             }
40              
41              
42             sub get_logtime {
43 0     0 0   my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime();
44 0           return sprintf("[%04d-%02d-%02d %02d:%02d:%02d]", $year+1900, $mon+1, $mday, $hour, $min, $sec);
45             }
46              
47             sub main {
48 0     0 0   my $cmdargs = cmdargs({ valuableArgs => 0, noCommand => 1, optionAtAll => 0 }, @_);
49 0 0 0       if (defined($cmdargs->{'-h'}) or defined($cmdargs->{'--help'}))
50             {
51 0           my @lines;
52 0           @lines = get_pod_text(dirname(__FILE__)."/nodie/nodie.pl", "SYNOPSIS");
53 0 0         @lines = get_pod_text(dirname(__FILE__)."/nodie/nodie.pl", "ABSTRACT") unless defined($lines[0]);
54 0           $lines[0] = "nodie.pl";
55 0           say join("\n", @lines);
56 0           return 0;
57             }
58 0           my $arg_exitcodes = $cmdargs->{'-e'};
59 0 0         $arg_exitcodes = $cmdargs->{'--exitcodes'} unless defined($arg_exitcodes);
60 0 0         $arg_exitcodes = "" unless defined($arg_exitcodes);
61 0           my @exitcodes = split(/\s*,\s*/, $arg_exitcodes);
62 0           my %exitcodes = array_to_hash(@exitcodes);
63 0           while (my $key = each %exitcodes) {
64 0           my $value = $exitcodes{$key};
65 0 0 0       unless (looks_like_number($value) and $value == int($value) and $value >= 0) {
      0        
66 0           delete $exitcodes{$key};
67 0           next;
68             }
69 0           $exitcodes{$key} = int($value);
70             }
71 0           @exitcodes = values %exitcodes;
72 0 0         push @exitcodes, 0, 2 unless @exitcodes;
73 0           my $arg_log = $cmdargs->{'-l'};
74 0 0         $arg_log = $cmdargs->{'--log'} unless defined($arg_log);
75 0           my $log_fh;
76 0 0         if (defined($arg_log)) {
77 0 0         $arg_log = "&STDERR" if $arg_log =~ /^\s*$/;
78 0 0         $arg_log = "&STDOUT" if $arg_log =~ /^\s*\-\s*$/;
79 0           my $mode = "";
80 0 0         if ($arg_log =~ /^&(.*)$/) {
81 0           $mode .= "&";
82 0           $arg_log = $1;
83             }
84 0 0         open($log_fh, ">>".$mode, $arg_log) or undef($log_fh);
85 0 0         warn "Can't open log file $mode$arg_log: $!\n" unless defined($log_fh);
86             }
87 0           my @params = (@{$cmdargs->{parameters}}, @{$cmdargs->{late_parameters}});
  0            
  0            
88 0 0         die "command is not specified\n" unless @params;
89 0           my $exitcode;
90 0           do {
91 0 0         sleep 1 if defined($exitcode);
92 0 0         print $log_fh get_logtime()." ".(defined($exitcode)? "Restarting": "Starting")."...\n" if defined($log_fh);
    0          
93 0 0         sleep 1 if defined($exitcode);
94 0           $exitcode = system2(@params);
95 0 0         die "$!\n" if $exitcode < 0;
96 0 0         print $log_fh get_logtime()." Returned exit code: $exitcode\n" if defined($log_fh);
97             } while (not grep(/^$exitcode$/, @exitcodes));
98 0           return $exitcode;
99             }
100              
101             sub run {
102 0     0 0   return main(@ARGV);
103             }
104              
105              
106             1;
107             __END__