/usr/share/perl5/Rex/Commands/Cron.pm is in rex 1.4.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | #
# (c) Jan Gehring <jan.gehring@gmail.com>
#
# vim: set ts=2 sw=2 tw=0:
# vim: set expandtab:
=head1 NAME
Rex::Commands::Cron - Simple Cron Management
=head1 DESCRIPTION
With this Module you can manage your cronjobs.
=head1 SYNOPSIS
use Rex::Commands::Cron;
cron add => "root", {
minute => '5',
hour => '*',
day_of_month => '*',
month => '*',
day_of_week => '*',
command => '/path/to/your/cronjob',
};
cron list => "root";
cron delete => "root", 3;
=head1 EXPORTED FUNCTIONS
=cut
package Rex::Commands::Cron;
use strict;
use warnings;
our $VERSION = '1.4.1'; # VERSION
require Rex::Exporter;
use base qw(Rex::Exporter);
use vars qw(@EXPORT);
use Carp;
use Rex::Cron;
use Data::Dumper;
@EXPORT = qw(cron cron_entry);
=head2 cron_entry($name, %option)
Manage cron entries.
cron_entry "reload-httpd",
ensure => "present",
command => "/etc/init.d/httpd restart",
minute => "1,5",
hour => "11,23",
month => "1,5",
day_of_week => "1,3",
day_of_month => "1,3,5",
user => "root",
on_change => sub { say "cron added"; };
# remove an entry
cron_entry "reload-httpd",
ensure => "absent",
command => "/etc/init.d/httpd restart",
minute => "1,5",
hour => "11,23",
month => "1,5",
day_of_week => "1,3",
day_of_month => "1,3,5",
user => "root",
on_change => sub { say "cron removed."; };
=cut
sub cron_entry {
my ( $name, %option ) = @_;
$option{ensure} ||= "present";
confess "No 'user' given." if ( !exists $option{user} );
Rex::get_current_connection()->{reporter}
->report_resource_start( type => "cron_entry", name => $name );
my $changed = 0;
my $ensure = $option{ensure};
if ( $option{ensure} eq "present" ) {
Rex::Logger::debug("Creating new cron_entry: $name");
my $user = $option{user};
delete $option{user};
delete $option{ensure};
$changed = &cron( add => $user, \%option );
}
elsif ( $option{ensure} eq "absent" ) {
Rex::Logger::debug("Removing cron_entry: $name");
my $user = $option{user};
my $c = Rex::Cron->create();
%option = $c->_create_defaults(%option);
my @crons = &cron( list => $user );
my $i = 0;
my $cron_id;
for my $cron (@crons) {
if ( $cron->{minute} eq $option{minute}
&& $cron->{hour} eq $option{hour}
&& $cron->{month} eq $option{month}
&& $cron->{day_of_week} eq $option{day_of_week}
&& $cron->{day_of_month} eq $option{day_of_month}
&& $cron->{command} eq $option{command} )
{
# cron found
$cron_id = $i;
last;
}
$i++;
}
delete $option{user};
delete $option{ensure};
if ( defined $cron_id ) {
&cron( delete => $user, $cron_id );
$changed = 1;
}
else {
Rex::Logger::debug("Cron $name not found.");
Rex::Logger::debug( Dumper( \%option ) );
}
}
if ($changed) {
if ( exists $option{on_change} && ref $option{on_change} eq "CODE" ) {
$option{on_change}->( $name, %option );
}
Rex::get_current_connection()->{reporter}->report(
changed => 1,
message => "Resource cron_entry status changed to $ensure."
);
}
else {
Rex::get_current_connection()->{reporter}->report( changed => 0, );
}
Rex::get_current_connection()->{reporter}
->report_resource_end( type => "cron_entry", name => $name );
}
=head2 cron($action => $user, ...)
With this function you can manage cronjobs.
List cronjobs.
use Rex::Commands::Cron;
use Data::Dumper;
task "listcron", "server1", sub {
my @crons = cron list => "root";
print Dumper(\@crons);
};
Add a cronjob.
This example will add a cronjob running on minute 1, 5, 19 and 40. Every hour and every day.
use Rex::Commands::Cron;
use Data::Dumper;
task "addcron", "server1", sub {
cron add => "root", {
minute => "1,5,19,40",
command => '/path/to/your/cronjob',
};
};
This example will add a cronjob running on the 1st, 3rd and 5th day of January and May, but only when it's a Monday or Wednesday. On those days, the job will run when the hour is 11 or 23, and the minute is 1 or 5 (in other words at 11:01, 11:05, 23:01 and 23:05).
task "addcron", "server1", sub {
cron add => "root", {
minute => "1,5",
hour => "11,23",
month => "1,5",
day_of_week => "1,3",
day_of_month => "1,3,5",
command => '/path/to/your/cronjob',
};
};
Delete a cronjob.
This example will delete the 4th cronjob. Counting starts with zero (0).
task "delcron", "server1", sub {
cron delete => "root", 3;
};
Managing Environment Variables inside cron.
task "mycron", "server1", sub {
cron env => user => add => {
MYVAR => "foo",
};
cron env => user => delete => $index;
cron env => user => delete => 1;
cron env => user => "list";
};
=cut
sub cron {
my ( $action, $user, $config, @more ) = @_;
my $c = Rex::Cron->create();
$c->read_user_cron($user); # this must always be the first action
if ( $action eq "list" ) {
return $c->list_jobs;
}
elsif ( $action eq "add" ) {
if ( $c->add( %{$config} ) ) {
my $rnd_file = $c->write_cron;
$c->activate_user_cron( $rnd_file, $user );
return 1; # something changed
}
return 0; # nothing changed
}
elsif ( $action eq "delete" ) {
my $to_delete = $config;
$c->delete_job($to_delete);
my $rnd_file = $c->write_cron;
$c->activate_user_cron( $rnd_file, $user );
}
elsif ( $action eq "env" ) {
my $env_action = $config;
if ( $env_action eq "add" ) {
my $data = shift @more;
for my $key ( keys %{$data} ) {
$c->add_env( $key => $data->{$key}, );
}
my $rnd_file = $c->write_cron;
$c->activate_user_cron( $rnd_file, $user );
}
elsif ( $env_action eq "list" ) {
return $c->list_envs;
}
elsif ( $env_action eq "delete" ) {
my $num = shift @more;
$c->delete_env($num);
my $rnd_file = $c->write_cron;
$c->activate_user_cron( $rnd_file, $user );
}
}
}
1;
|