Crontab report on what works at the given start and end of datetime

Are there any tools or reports out there that, when using the crontab file, can output which tasks are completed over a period of time.

Our crontab file has become very large, and our system administrators are struggling to figure out what work tasks need to be restarted when we have scheduled downtime on the server. We are trying to figure out what tasks we need to complete.

I planned to write my own script, but wondered if there was anything there already

+3
source share
3 answers

One thing you can do is:

  • Perl Schedule:: Cron

  • , ( " " ( ), . $now = time; $now++.

  • , .

  • Perl, crontab -l contab, , cmd1 arg1 arg2 perl sub { print "Execution: cmd1 arg1 arg2\n"}

  • , POD.

    crontab .

+1

" " Schedule:: Cron ( , , , , Schedule:: Cron. Time:: Mock , ,

perl -MTime::Mock=throttle,600 schedule.pl

" " 600 (, , 10 , ). . Man- Time:: Mock.

crontab Schedule:: Cron README:

 use Schedule::Cron;
 my $cron = new Schedule::Cron(sub { system(shift) },
                               file => "/var/spool/crontab.perl");
 $cron->run();

, system() . , , . , print.

+1

DVK, Perl Schedule:: Cron:: Events.

" " - . crontab a_crontab.txt:

59 21 * * 1-5    ls >> $HOME/work/stack_overflow/cron_ls.txt
    # A comment
18 09 * * 1-5    echo "wibble"

script cron.pl :

$ perl cron.pl a_crontab.txt "2009/11/09 00:00:00" "2009/11/12 00:00:00"
2009/11/09 09:18:00 "echo "wibble""
2009/11/09 21:59:00 "ls >> $HOME/work/stack_overflow/cron_ls.txt"
2009/11/10 09:18:00 "echo "wibble""
2009/11/10 21:59:00 "ls >> $HOME/work/stack_overflow/cron_ls.txt"
2009/11/11 09:18:00 "echo "wibble""
2009/11/11 21:59:00 "ls >> $HOME/work/stack_overflow/cron_ls.txt"
2009/11/12 09:18:00 "echo "wibble""
2009/11/12 21:59:00 "ls >> $HOME/work/stack_overflow/cron_ls.txt"

(!) script:

use strict;
use warnings;

use Schedule::Cron::Events;

my $crontab_file = shift || die "! Must provide crontab file name";
my $start_time   = shift || die "! Must provide start time YYYY/MM/DD HH:MM:SS";
my $stop_time    = shift || die "! Must provide stop time YYYY/MM/DD HH:MM:SS";

open my $fh, '<', $crontab_file or die "! Could not open file $crontab_file for reading: $!";
my $table = [];
while ( <$fh> ) { 
    next if /^\s*$/;
    next if /^\s*#/;

    chomp;

    push @$table, new Schedule::Cron::Events( $_, Date => [ smhdmy_from_iso( $start_time ) ] );
}
close $fh;

my $events = [];
for my $cron ( @$table ) {
    my $event_time = $stop_time;
    while ( $event_time le $stop_time ) {
        my ( $sec, $min, $hour, $day, $month, $year ) = $cron->nextEvent;
        $event_time = sprintf q{%4d/%02d/%02d %02d:%02d:%02d}, 1900 + $year, 1 + $month, $day, $hour, $min, $sec;

        push @$events, qq{$event_time "} . $cron->commandLine . q{"};
    }
}

print join( qq{\n}, sort @$events ) . qq{\n};

sub smhdmy_from_iso {
    my $input = shift;

    my ( $y, $m, $d, $H, $M, $S ) = ( $input =~ m=(\d{4})/(\d\d)/(\d\d) (\d\d):(\d\d):(\d\d)= );

     ( $S, $M, $H, $d, --$m, $y - 1900 );
}

, .

0

Source: https://habr.com/ru/post/1722376/


All Articles