| 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.01 | 
| 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 |  | 13491 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 26 |  | 
| 23 | 1 |  |  | 1 |  | 4 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 21 |  | 
| 24 | 1 |  |  | 1 |  | 12 | use v5.10.1; | 
|  | 1 |  |  |  |  | 6 |  | 
| 25 | 1 |  |  | 1 |  | 8 | use feature qw(switch); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 92 |  | 
| 26 | 1 |  |  | 1 |  | 522 | no if ($] >= 5.018), 'warnings' => 'experimental'; | 
|  | 1 |  |  |  |  | 11 |  | 
|  | 1 |  |  |  |  | 4 |  | 
| 27 | 1 |  |  | 1 |  | 575 | use FindBin; | 
|  | 1 |  |  |  |  | 820 |  | 
|  | 1 |  |  |  |  | 37 |  | 
| 28 | 1 |  |  | 1 |  | 6 | use File::Basename; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 40 |  | 
| 29 | 1 |  |  | 1 |  | 5 | use Scalar::Util qw(looks_like_number); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 56 |  | 
| 30 | 1 |  |  | 1 |  | 394 | use Lazy::Utils; | 
|  | 1 |  |  |  |  | 48627 |  | 
|  | 1 |  |  |  |  | 128 |  | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | BEGIN { | 
| 34 | 1 |  |  | 1 |  | 8 | require Exporter; | 
| 35 | 1 |  |  |  |  | 3 | our $VERSION     = '1.01'; | 
| 36 | 1 |  |  |  |  | 7 | our @ISA         = qw(Exporter); | 
| 37 | 1 |  |  |  |  | 2 | our @EXPORT      = qw(main run); | 
| 38 | 1 |  |  |  |  | 660 | 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 |  |  |  |  |  | while (my ($key, $value) = each @exitcodes) { | 
| 63 | 0 | 0 | 0 |  |  |  | unless (looks_like_number($value) and $value == int($value) and $value >= 0) { | 
|  |  |  | 0 |  |  |  |  | 
| 64 | 0 |  |  |  |  |  | delete $exitcodes[$key]; | 
| 65 | 0 |  |  |  |  |  | next; | 
| 66 |  |  |  |  |  |  | } | 
| 67 | 0 |  |  |  |  |  | $exitcodes[$key] = int($value); | 
| 68 |  |  |  |  |  |  | } | 
| 69 | 0 | 0 |  |  |  |  | push @exitcodes, 0, 2 unless @exitcodes; | 
| 70 | 0 |  |  |  |  |  | my $arg_log = $cmdargs->{'-l'}; | 
| 71 | 0 | 0 |  |  |  |  | $arg_log = $cmdargs->{'--log'} unless defined($arg_log); | 
| 72 | 0 |  |  |  |  |  | my $log_fh; | 
| 73 | 0 | 0 |  |  |  |  | if (defined($arg_log)) { | 
| 74 | 0 | 0 |  |  |  |  | $arg_log = "&STDERR" if $arg_log =~ /^\s*$/; | 
| 75 | 0 | 0 |  |  |  |  | $arg_log = "&STDOUT" if $arg_log =~ /^\s*\-\s*$/; | 
| 76 | 0 |  |  |  |  |  | my $mode = ""; | 
| 77 | 0 | 0 |  |  |  |  | if ($arg_log =~ /^&(.*)$/) { | 
| 78 | 0 |  |  |  |  |  | $mode .= "&"; | 
| 79 | 0 |  |  |  |  |  | $arg_log = $1; | 
| 80 |  |  |  |  |  |  | } | 
| 81 | 0 | 0 |  |  |  |  | open($log_fh, ">>".$mode, $arg_log) or undef($log_fh); | 
| 82 | 0 | 0 |  |  |  |  | warn "Can't open log file $mode$arg_log: $!\n" unless defined($log_fh); | 
| 83 |  |  |  |  |  |  | } | 
| 84 | 0 |  |  |  |  |  | my $exitcode; | 
| 85 | 0 |  |  |  |  |  | do { | 
| 86 | 0 | 0 |  |  |  |  | sleep 1 if defined($exitcode); | 
| 87 | 0 | 0 |  |  |  |  | print $log_fh get_logtime()." ".(defined($exitcode)? "Restarting": "Starting")."...\n" if defined($log_fh); | 
|  |  | 0 |  |  |  |  |  | 
| 88 | 0 | 0 |  |  |  |  | sleep 1 if defined($exitcode); | 
| 89 | 0 |  |  |  |  |  | $exitcode = system2(@{$cmdargs->{parameters}}, @{$cmdargs->{late_parameters}}); | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 90 | 0 | 0 |  |  |  |  | die "$!\n" if $exitcode < 0; | 
| 91 | 0 | 0 |  |  |  |  | print $log_fh get_logtime()." Returned exit code: $exitcode\n" if defined($log_fh); | 
| 92 |  |  |  |  |  |  | } while (not grep(/^$exitcode$/, @exitcodes)); | 
| 93 | 0 |  |  |  |  |  | return $exitcode; | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | sub run { | 
| 97 | 0 |  |  | 0 | 0 |  | return main(@ARGV); | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | 1; | 
| 102 |  |  |  |  |  |  | __END__ |