| 1 | #!/usr/bin/env perl | ||||||||||
| 2 | package ubic_periodic; | ||||||||||
| 3 | 1 | $ubic_periodic::VERSION = '1.59'; | |||||||||
| 4 | # ABSTRACT: run given command every N seconds | ||||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | |||||||||||
| 10 | 1 | ▶ | use Getopt::Long 2.33; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use Pod::Usage; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 1 | ▶ | return 1 if caller; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 14 | |||||||||||
| 15 | 0 | my $period = 60; | |||||||||
| 16 | 0 | my $stdout; | |||||||||
| 17 | my $stderr; | ||||||||||
| 18 | 0 | my $rotate_logs; | |||||||||
| 19 | |||||||||||
| 20 | 0 | ▶ | GetOptions( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | 'period=i' => \$period, | ||||||||||
| 22 | 'stdout=s' => \$stdout, | ||||||||||
| 23 | 'stderr=s' => \$stderr, | ||||||||||
| 24 | 'rotate-logs!' => \$rotate_logs, | ||||||||||
| 25 | ) or pod2usage(2); | ||||||||||
| 26 | 0 | ▶ | pod2usage(2) unless @ARGV == 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | |||||||||||
| 28 | 0 | my $command = shift @ARGV; | |||||||||
| 29 | |||||||||||
| 30 | sub xunlink { | ||||||||||
| 31 | 0 | ▶ | my $file = shift; | ||||||||
|
Subroutine
xunlink: not called CC=2 SCAR=17.9
| |||||||||||
| 32 | 0 | ▶ | unlink $file or die "Can't unlink '$file': $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | sub xrename { | ||||||||||
| 36 | 0 | ▶ | my ($from, $to) = @_; | ||||||||
|
Subroutine
xrename: not called CC=2 SCAR=17.9
| |||||||||||
| 37 | 0 | ▶ | rename $from => $to or die "Can't rename '$from' to '$to': $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | sub touch { | ||||||||||
| 41 | 0 | ▶ | my $file = shift; | ||||||||
|
Subroutine
touch: not called CC=2 SCAR=17.9
| |||||||||||
| 42 | 0 | ▶ | open my $fh, '>', $file or die "Can't touch '$file': $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | sub rotate { | ||||||||||
| 46 | 0 | ▶ | my $file = shift; | ||||||||
|
Subroutine
rotate: not called CC=5 SCAR=34.0
| |||||||||||
| 47 | 0 | my $time = time; | |||||||||
| 48 | |||||||||||
| 49 | 0 | ▶ | return unless -e $file; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | |||||||||||
| 51 | 0 | ▶ | unless (-e "$file.1") { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 0 | touch("$file.1"); | |||||||||
| 53 | } | ||||||||||
| 54 | 0 | my $mtime = (stat "$file.1")[9]; | |||||||||
| 55 | 0 | ▶ | if ($time - $mtime >= 86400) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | # it's time to rotate! | ||||||||||
| 57 | 0 | ▶ | if (-e "$file.2") { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 0 | xunlink("$file.2"); | |||||||||
| 59 | } | ||||||||||
| 60 | 0 | xrename("$file.1" => "$file.2"); | |||||||||
| 61 | 0 | xrename($file => "$file.1"); | |||||||||
| 62 | } | ||||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 0 | while (1) { | |||||||||
| 66 | 0 | my $start_time = time; | |||||||||
| 67 | |||||||||||
| 68 | 0 | ▶ | if ($rotate_logs) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 0 | rotate($stdout); | |||||||||
| 70 | 0 | rotate($stderr); | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | # we reopen logs on every loop, so we don't have to restart periodic service on logrotate | ||||||||||
| 74 | 0 | ▶ | if ($stdout) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 0 | ▶ | open STDOUT, '>>', $stdout or die "Can't open stdout: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | } | ||||||||||
| 77 | 0 | ▶ | if ($stderr) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 0 | ▶ | open STDERR, '>>', $stderr or die "Can't open stderr: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | } | ||||||||||
| 80 | 0 | system($command); # no, we don't check for failures | |||||||||
| 81 | |||||||||||
| 82 | 0 | my $time = time; | |||||||||
| 83 | 0 | ▶ | if ($time - $start_time < $period) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 0 | sleep $start_time + $period - $time; | |||||||||
| 85 | } | ||||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | __END__ | ||||||||||