| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# |
|
2
|
|
|
|
|
|
|
# (c) Jan Gehring |
|
3
|
|
|
|
|
|
|
# |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Rex::Cron::Base; |
|
6
|
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
69126
|
use v5.12.5; |
|
|
2
|
|
|
|
|
17
|
|
|
8
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
|
2
|
|
|
|
|
8
|
|
|
|
2
|
|
|
|
|
108
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '1.14.2.3'; # TRIAL VERSION |
|
11
|
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
423
|
use Rex::Logger; |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
48
|
|
|
13
|
2
|
|
|
2
|
|
620
|
use Rex::Commands; |
|
|
2
|
|
|
|
|
9
|
|
|
|
2
|
|
|
|
|
15
|
|
|
14
|
2
|
|
|
2
|
|
19
|
use Rex::Commands::File; |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
21
|
|
|
15
|
2
|
|
|
2
|
|
16
|
use Rex::Commands::Fs; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
16
|
|
|
16
|
2
|
|
|
2
|
|
15
|
use Rex::Helper::Run; |
|
|
2
|
|
|
|
|
7
|
|
|
|
2
|
|
|
|
|
131
|
|
|
17
|
2
|
|
|
2
|
|
13
|
use Data::Dumper; |
|
|
2
|
|
|
|
|
6
|
|
|
|
2
|
|
|
|
|
85
|
|
|
18
|
2
|
|
|
2
|
|
26
|
use Rex::Helper::Path; |
|
|
2
|
|
|
|
|
5
|
|
|
|
2
|
|
|
|
|
3839
|
|
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub new { |
|
21
|
2
|
|
|
2
|
0
|
1684
|
my $that = shift; |
|
22
|
2
|
|
33
|
|
|
15
|
my $proto = ref($that) || $that; |
|
23
|
2
|
|
|
|
|
8
|
my $self = {@_}; |
|
24
|
|
|
|
|
|
|
|
|
25
|
2
|
|
|
|
|
5
|
bless( $self, $proto ); |
|
26
|
|
|
|
|
|
|
|
|
27
|
2
|
|
|
|
|
14
|
return $self; |
|
28
|
|
|
|
|
|
|
} |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub list { |
|
31
|
6
|
|
|
6
|
0
|
35
|
my ($self) = @_; |
|
32
|
6
|
|
|
|
|
10
|
return @{ $self->{cron} }; |
|
|
6
|
|
|
|
|
52
|
|
|
33
|
|
|
|
|
|
|
} |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub list_jobs { |
|
36
|
4
|
|
|
4
|
0
|
10151
|
my ($self) = @_; |
|
37
|
4
|
|
|
|
|
10
|
my @jobs = @{ $self->{cron} }; |
|
|
4
|
|
|
|
|
17
|
|
|
38
|
|
|
|
|
|
|
my @ret = |
|
39
|
25
|
|
|
|
|
40
|
map { { line => $_->{line}, %{ $_->{cron} } } } |
|
|
25
|
|
|
|
|
130
|
|
|
40
|
4
|
|
|
|
|
9
|
grep { $_->{type} eq "job" } @jobs; |
|
|
69
|
|
|
|
|
128
|
|
|
41
|
|
|
|
|
|
|
} |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub list_envs { |
|
44
|
6
|
|
|
6
|
0
|
3870
|
my ($self) = @_; |
|
45
|
6
|
|
|
|
|
10
|
my @jobs = @{ $self->{cron} }; |
|
|
6
|
|
|
|
|
20
|
|
|
46
|
6
|
|
|
|
|
14
|
my @ret = grep { $_->{type} eq "env" } @jobs; |
|
|
94
|
|
|
|
|
169
|
|
|
47
|
|
|
|
|
|
|
} |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub add { |
|
50
|
4
|
|
|
4
|
0
|
32623
|
my ( $self, %config ) = @_; |
|
51
|
|
|
|
|
|
|
|
|
52
|
4
|
|
|
|
|
17
|
%config = $self->_create_defaults(%config); |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
my $new_cron = sprintf( |
|
55
|
|
|
|
|
|
|
"%s %s %s %s %s %s", |
|
56
|
|
|
|
|
|
|
$config{"minute"}, $config{"hour"}, $config{"day_of_month"}, |
|
57
|
4
|
|
|
|
|
21
|
$config{"month"}, $config{"day_of_week"}, $config{"command"}, |
|
58
|
|
|
|
|
|
|
); |
|
59
|
|
|
|
|
|
|
|
|
60
|
4
|
|
|
|
|
6
|
my $dupe = grep { $_->{line} eq $new_cron } @{ $self->{cron} }; |
|
|
62
|
|
|
|
|
106
|
|
|
|
4
|
|
|
|
|
10
|
|
|
61
|
4
|
50
|
|
|
|
12
|
if ($dupe) { |
|
62
|
0
|
|
|
|
|
0
|
Rex::Logger::debug("Job \"$new_cron\" already installed, skipping."); |
|
63
|
0
|
|
|
|
|
0
|
return 0; |
|
64
|
|
|
|
|
|
|
} |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
push( |
|
67
|
4
|
|
|
|
|
7
|
@{ $self->{cron} }, |
|
|
4
|
|
|
|
|
13
|
|
|
68
|
|
|
|
|
|
|
{ |
|
69
|
|
|
|
|
|
|
type => "job", |
|
70
|
|
|
|
|
|
|
line => $new_cron, |
|
71
|
|
|
|
|
|
|
cron => \%config, |
|
72
|
|
|
|
|
|
|
} |
|
73
|
|
|
|
|
|
|
); |
|
74
|
|
|
|
|
|
|
|
|
75
|
4
|
|
|
|
|
11
|
return 1; |
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub add_env { |
|
79
|
2
|
|
|
2
|
0
|
42183
|
my ( $self, $name, $value ) = @_; |
|
80
|
|
|
|
|
|
|
|
|
81
|
2
|
|
|
|
|
5
|
my $env_index = 0; |
|
82
|
2
|
|
|
|
|
4
|
my $exists = 0; |
|
83
|
2
|
|
|
|
|
8
|
for my $env ( $self->list_envs ) { |
|
84
|
7
|
50
|
|
|
|
18
|
if ( $env->{name} eq "$name" ) { |
|
85
|
0
|
0
|
|
|
|
0
|
if ( $env->{value} ne "\"$value\"" ) { |
|
86
|
0
|
|
|
|
|
0
|
Rex::Logger::debug("Environment variable changed : $name"); |
|
87
|
0
|
|
|
|
|
0
|
$self->delete_env($env_index); |
|
88
|
|
|
|
|
|
|
} |
|
89
|
|
|
|
|
|
|
else { |
|
90
|
0
|
|
|
|
|
0
|
Rex::Logger::debug( |
|
91
|
|
|
|
|
|
|
"Environment variable already exists with same value: $name=$value"); |
|
92
|
0
|
|
|
|
|
0
|
$exists = 1; |
|
93
|
|
|
|
|
|
|
} |
|
94
|
|
|
|
|
|
|
} |
|
95
|
|
|
|
|
|
|
|
|
96
|
7
|
|
|
|
|
9
|
$env_index++; |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
|
|
99
|
2
|
50
|
|
|
|
8
|
if ( $exists == 0 ) { |
|
100
|
|
|
|
|
|
|
unshift( |
|
101
|
2
|
|
|
|
|
4
|
@{ $self->{cron} }, |
|
|
2
|
|
|
|
|
16
|
|
|
102
|
|
|
|
|
|
|
{ |
|
103
|
|
|
|
|
|
|
type => "env", |
|
104
|
|
|
|
|
|
|
line => "$name=\"$value\"", |
|
105
|
|
|
|
|
|
|
name => $name, |
|
106
|
|
|
|
|
|
|
value => $value, |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
); |
|
109
|
|
|
|
|
|
|
} |
|
110
|
|
|
|
|
|
|
} |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub delete_job { |
|
113
|
2
|
|
|
2
|
0
|
12216
|
my ( $self, $num ) = @_; |
|
114
|
2
|
|
|
|
|
6
|
my @jobs = $self->list_jobs; |
|
115
|
|
|
|
|
|
|
|
|
116
|
2
|
|
|
|
|
4
|
my $i = 0; |
|
117
|
2
|
|
|
|
|
5
|
my $to_delete; |
|
118
|
2
|
|
|
|
|
4
|
for my $j ( @{ $self->{cron} } ) { |
|
|
2
|
|
|
|
|
7
|
|
|
119
|
23
|
100
|
|
|
|
43
|
if ( $j->{line} eq $jobs[$num]->{line} ) { |
|
120
|
2
|
|
|
|
|
3
|
$to_delete = $i; |
|
121
|
2
|
|
|
|
|
5
|
last; |
|
122
|
|
|
|
|
|
|
} |
|
123
|
|
|
|
|
|
|
|
|
124
|
21
|
|
|
|
|
27
|
$i++; |
|
125
|
|
|
|
|
|
|
} |
|
126
|
|
|
|
|
|
|
|
|
127
|
2
|
50
|
|
|
|
9
|
unless ( defined $to_delete ) { |
|
128
|
0
|
|
|
|
|
0
|
die("Cron Entry $num not found."); |
|
129
|
|
|
|
|
|
|
} |
|
130
|
|
|
|
|
|
|
|
|
131
|
2
|
|
|
|
|
7
|
$self->delete($to_delete); |
|
132
|
|
|
|
|
|
|
} |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub delete_env { |
|
135
|
2
|
|
|
2
|
0
|
5729
|
my ( $self, $num ) = @_; |
|
136
|
|
|
|
|
|
|
|
|
137
|
2
|
|
|
|
|
8
|
my @jobs = $self->list_envs; |
|
138
|
|
|
|
|
|
|
|
|
139
|
2
|
|
|
|
|
17
|
my $i = 0; |
|
140
|
2
|
|
|
|
|
3
|
my $to_delete; |
|
141
|
2
|
|
|
|
|
5
|
for my $j ( @{ $self->{cron} } ) { |
|
|
2
|
|
|
|
|
6
|
|
|
142
|
3
|
100
|
|
|
|
18
|
if ( $j->{line} eq $jobs[$num]->{line} ) { |
|
143
|
2
|
|
|
|
|
5
|
$to_delete = $i; |
|
144
|
2
|
|
|
|
|
5
|
last; |
|
145
|
|
|
|
|
|
|
} |
|
146
|
|
|
|
|
|
|
|
|
147
|
1
|
|
|
|
|
3
|
$i++; |
|
148
|
|
|
|
|
|
|
} |
|
149
|
|
|
|
|
|
|
|
|
150
|
2
|
50
|
|
|
|
22
|
unless ( defined $to_delete ) { |
|
151
|
0
|
|
|
|
|
0
|
die("Cron Entry $num not found."); |
|
152
|
|
|
|
|
|
|
} |
|
153
|
|
|
|
|
|
|
|
|
154
|
2
|
|
|
|
|
7
|
$self->delete($to_delete); |
|
155
|
|
|
|
|
|
|
} |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
sub delete { |
|
158
|
6
|
|
|
6
|
0
|
53001
|
my ( $self, $num ) = @_; |
|
159
|
6
|
|
|
|
|
8
|
splice( @{ $self->{cron} }, $num, 1 ); |
|
|
6
|
|
|
|
|
28
|
|
|
160
|
|
|
|
|
|
|
} |
|
161
|
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
# returns a filename where the new cron is written to |
|
163
|
|
|
|
|
|
|
# after that the cronfile must be activated |
|
164
|
|
|
|
|
|
|
sub write_cron { |
|
165
|
1
|
|
|
1
|
0
|
20452
|
my ($self) = @_; |
|
166
|
|
|
|
|
|
|
|
|
167
|
1
|
|
|
|
|
10
|
my $rnd_file = get_tmp_file; |
|
168
|
|
|
|
|
|
|
|
|
169
|
1
|
|
|
|
|
5
|
my @lines = map { $_->{line} } @{ $self->{cron} }; |
|
|
18
|
|
|
|
|
31
|
|
|
|
1
|
|
|
|
|
5
|
|
|
170
|
|
|
|
|
|
|
|
|
171
|
1
|
|
|
|
|
13
|
my $fh = file_write $rnd_file; |
|
172
|
1
|
|
|
|
|
15
|
$fh->write( join( "\n", @lines ) . "\n" ); |
|
173
|
1
|
|
|
|
|
16
|
$fh->close; |
|
174
|
|
|
|
|
|
|
|
|
175
|
1
|
|
|
|
|
13
|
return $rnd_file; |
|
176
|
|
|
|
|
|
|
} |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub activate_user_cron { |
|
179
|
0
|
|
|
0
|
0
|
0
|
my ( $self, $file, $user ) = @_; |
|
180
|
0
|
0
|
|
|
|
0
|
$user = undef if $user eq &_whoami; |
|
181
|
|
|
|
|
|
|
|
|
182
|
0
|
|
|
|
|
0
|
my $command = 'crontab'; |
|
183
|
0
|
0
|
|
|
|
0
|
$command .= " -u $user" if defined $user; |
|
184
|
|
|
|
|
|
|
|
|
185
|
0
|
|
|
|
|
0
|
i_run "$command $file"; |
|
186
|
0
|
|
|
|
|
0
|
unlink $file; |
|
187
|
|
|
|
|
|
|
} |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
sub read_user_cron { |
|
190
|
0
|
|
|
0
|
0
|
0
|
my ( $self, $user ) = @_; |
|
191
|
0
|
0
|
|
|
|
0
|
$user = undef if $user eq &_whoami; |
|
192
|
|
|
|
|
|
|
|
|
193
|
0
|
|
|
|
|
0
|
my $command = 'crontab -l'; |
|
194
|
0
|
0
|
|
|
|
0
|
$command .= " -u $user" if defined $user; |
|
195
|
0
|
|
|
|
|
0
|
$command .= ' 2> /dev/null'; |
|
196
|
|
|
|
|
|
|
|
|
197
|
0
|
|
|
|
|
0
|
my @lines = i_run $command, fail_ok => 1; |
|
198
|
0
|
|
|
|
|
0
|
$self->parse_cron(@lines); |
|
199
|
|
|
|
|
|
|
} |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
sub parse_cron { |
|
202
|
2
|
|
|
2
|
0
|
19
|
my ( $self, @lines ) = @_; |
|
203
|
|
|
|
|
|
|
|
|
204
|
2
|
|
|
|
|
7
|
chomp @lines; |
|
205
|
|
|
|
|
|
|
|
|
206
|
2
|
|
|
|
|
4
|
my @cron; |
|
207
|
|
|
|
|
|
|
|
|
208
|
2
|
|
|
|
|
7
|
for my $line (@lines) { |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
# comment |
|
211
|
32
|
100
|
|
|
|
131
|
if ( $line =~ m/^#/ ) { |
|
|
|
50
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
212
|
12
|
|
|
|
|
34
|
push( |
|
213
|
|
|
|
|
|
|
@cron, |
|
214
|
|
|
|
|
|
|
{ |
|
215
|
|
|
|
|
|
|
type => "comment", |
|
216
|
|
|
|
|
|
|
line => $line, |
|
217
|
|
|
|
|
|
|
} |
|
218
|
|
|
|
|
|
|
); |
|
219
|
|
|
|
|
|
|
} |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
# empty line |
|
222
|
|
|
|
|
|
|
elsif ( $line =~ m/^\s*$/ ) { |
|
223
|
0
|
|
|
|
|
0
|
push( |
|
224
|
|
|
|
|
|
|
@cron, |
|
225
|
|
|
|
|
|
|
{ |
|
226
|
|
|
|
|
|
|
type => "empty", |
|
227
|
|
|
|
|
|
|
line => $line, |
|
228
|
|
|
|
|
|
|
} |
|
229
|
|
|
|
|
|
|
); |
|
230
|
|
|
|
|
|
|
} |
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
# job |
|
233
|
|
|
|
|
|
|
elsif ( $line =~ m/^(@|\*|[0-9])/ ) { |
|
234
|
14
|
|
|
|
|
67
|
my ( $min, $hour, $day, $month, $dow, $cmd ) = split( /\s+/, $line, 6 ); |
|
235
|
14
|
|
|
|
|
76
|
push( |
|
236
|
|
|
|
|
|
|
@cron, |
|
237
|
|
|
|
|
|
|
{ |
|
238
|
|
|
|
|
|
|
type => "job", |
|
239
|
|
|
|
|
|
|
line => $line, |
|
240
|
|
|
|
|
|
|
cron => { |
|
241
|
|
|
|
|
|
|
minute => $min, |
|
242
|
|
|
|
|
|
|
hour => $hour, |
|
243
|
|
|
|
|
|
|
day_of_month => $day, |
|
244
|
|
|
|
|
|
|
month => $month, |
|
245
|
|
|
|
|
|
|
day_of_week => $dow, |
|
246
|
|
|
|
|
|
|
command => $cmd, |
|
247
|
|
|
|
|
|
|
}, |
|
248
|
|
|
|
|
|
|
} |
|
249
|
|
|
|
|
|
|
); |
|
250
|
|
|
|
|
|
|
} |
|
251
|
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
elsif ( $line =~ m/=/ ) { |
|
253
|
6
|
|
|
|
|
23
|
my ( $name, $value ) = split( /=/, $line, 2 ); |
|
254
|
6
|
|
|
|
|
16
|
$name =~ s/^\s+//; |
|
255
|
6
|
|
|
|
|
11
|
$name =~ s/\s+$//; |
|
256
|
6
|
|
|
|
|
13
|
$value =~ s/^\s+//; |
|
257
|
6
|
|
|
|
|
9
|
$value =~ s/\s+$//; |
|
258
|
|
|
|
|
|
|
|
|
259
|
6
|
|
|
|
|
26
|
push( |
|
260
|
|
|
|
|
|
|
@cron, |
|
261
|
|
|
|
|
|
|
{ |
|
262
|
|
|
|
|
|
|
type => "env", |
|
263
|
|
|
|
|
|
|
line => $line, |
|
264
|
|
|
|
|
|
|
name => $name, |
|
265
|
|
|
|
|
|
|
value => $value, |
|
266
|
|
|
|
|
|
|
} |
|
267
|
|
|
|
|
|
|
); |
|
268
|
|
|
|
|
|
|
} |
|
269
|
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
else { |
|
271
|
0
|
|
|
|
|
0
|
Rex::Logger::debug("Error parsing cron line: $line"); |
|
272
|
0
|
|
|
|
|
0
|
next; |
|
273
|
|
|
|
|
|
|
} |
|
274
|
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
} |
|
276
|
|
|
|
|
|
|
|
|
277
|
2
|
|
|
|
|
13
|
$self->{cron} = \@cron; |
|
278
|
2
|
|
|
|
|
8
|
return @cron; |
|
279
|
|
|
|
|
|
|
} |
|
280
|
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
sub _create_defaults { |
|
282
|
4
|
|
|
4
|
|
12
|
my ( $self, %config ) = @_; |
|
283
|
|
|
|
|
|
|
|
|
284
|
4
|
100
|
|
|
|
14
|
$config{"minute"} = "*" unless defined $config{minute}; |
|
285
|
4
|
100
|
|
|
|
10
|
$config{"hour"} = "*" unless defined $config{hour}; |
|
286
|
4
|
100
|
|
|
|
12
|
$config{"day_of_month"} = "*" unless defined $config{day_of_month}; |
|
287
|
4
|
100
|
|
|
|
12
|
$config{"month"} = "*" unless defined $config{month}; |
|
288
|
4
|
100
|
|
|
|
10
|
$config{"day_of_week"} = "*" unless defined $config{day_of_week}; |
|
289
|
4
|
|
100
|
|
|
15
|
$config{"command"} ||= "false"; |
|
290
|
|
|
|
|
|
|
|
|
291
|
4
|
|
|
|
|
22
|
return %config; |
|
292
|
|
|
|
|
|
|
} |
|
293
|
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
sub _whoami { |
|
295
|
0
|
|
|
0
|
|
|
return i_run q(perl -e 'print scalar getpwuid($<)'); |
|
296
|
|
|
|
|
|
|
} |
|
297
|
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
1; |