| 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<nodie.pl|https://metacpan.org/pod/distribution/App-nodie/lib/App/nodie/nodie.pl> | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use v5.10.1; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | ▶ | use feature qw(switch); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | ▶ | no if ($] >= 5.018), 'warnings' => 'experimental'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use FindBin; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | ▶ | use File::Basename; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 1 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | ▶ | use Lazy::Utils; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 1 | our $VERSION = '1.03'; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||
| 37 | 1 | our @EXPORT = qw(main run); | |||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | our @EXPORT_OK = qw(); | |||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | sub get_logtime { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_logtime: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 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 | 0 | ▶ | sub main { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
main: not called CC=26 SCAR=65.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | my $cmdargs = cmdargs({ valuableArgs => 0, noCommand => 1, optionAtAll => 0 }, @_); | |||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if (defined($cmdargs->{'-h'}) or defined($cmdargs->{'--help'})) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $cmdargs->{"-h"} or defined $cmdargs->{"--help"}
MC/DC0%0/2
defined $cmdargs->{"-h"} or defined $cmdargs->{"--help"}
defined $cmdargs->{"-h"}
defined $cmdargs->{"--help"}
Truth table0%0/3
A or B
A= defined $cmdargs->{"-h"}B= defined $cmdargs->{"--help"}
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | { | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | my @lines; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | @lines = get_pod_text(dirname(__FILE__)."/nodie/nodie.pl", "SYNOPSIS"); | |||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | @lines = get_pod_text(dirname(__FILE__)."/nodie/nodie.pl", "ABSTRACT") unless defined($lines[0]); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | ▶ | $arg_exitcodes = $cmdargs->{'--exitcodes'} unless defined($arg_exitcodes); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | $arg_exitcodes = "" unless defined($arg_exitcodes); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | my @exitcodes = split(/\s*,\s*/, $arg_exitcodes); | |||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | my %exitcodes = array_to_hash(@exitcodes); | |||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | while (my $key = each %exitcodes) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | my $value = $exitcodes{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | unless (looks_like_number($value) and $value == int($value) and $value >= 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
&looks_like_number($value) and $value == int $value and $value >= 0
&looks_like_number($value) and $value == int $value
MC/DC0%0/3
&looks_like_number($value) and $value == int $value and $value >= 0
&looks_like_number($value)
$value == int $value
$value >= 0
Truth table0%0/4
A and B and C
A= &looks_like_number($value)B= $value == int $valueC= $value >= 0
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | delete $exitcodes{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | $exitcodes{$key} = int($value); | |||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | @exitcodes = values %exitcodes; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | push @exitcodes, 0, 2 unless @exitcodes; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | my $arg_log = $cmdargs->{'-l'}; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | $arg_log = $cmdargs->{'--log'} unless defined($arg_log); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | my $log_fh; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if (defined($arg_log)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | $arg_log = "&STDERR" if $arg_log =~ /^\s*$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | $arg_log = "&STDOUT" if $arg_log =~ /^\s*\-\s*$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | my $mode = ""; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if ($arg_log =~ /^&(.*)$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | $mode .= "&"; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | $arg_log = $1; | |||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | open($log_fh, ">>".$mode, $arg_log) or undef($log_fh); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | warn "Can't open log file $mode$arg_log: $!\n" unless defined($log_fh); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | my @params = (@{$cmdargs->{parameters}}, @{$cmdargs->{late_parameters}}); | |||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | die "command is not specified\n" unless @params; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | my $exitcode; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | sleep 1 if defined($exitcode); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | print $log_fh get_logtime()." ".(defined($exitcode)? "Restarting": "Starting")."...\n" if defined($log_fh); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | sleep 1 if defined($exitcode); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | $exitcode = system2(@params); | |||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | die "$!\n" if $exitcode < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | print $log_fh get_logtime()." Returned exit code: $exitcode\n" if defined($log_fh); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | } while (not grep(/^$exitcode$/, @exitcodes)); | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | return $exitcode; | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | return main(@ARGV); | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 107 | __END__ | ||||||||||||||||||||||||||||||||||||||||||