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