Config-Crontab

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

Config::Crontab will do basic regular expression syntax checks; it
won't allow obvious garbage (e.g., 'F * * * * /bin/bar' would fail
since 'F' is not valid), but it won't check for nonsensical entries
such as '10-2 * * * * /bin/bar' (the minute range specified is
invalid). However, if you attempt to commit an invalid crontab, the
'write' method will return undef and the 'error' method will be set
(with the output of crontab(1)) if the crontab is rejected by the
crontab program.

EXAMPLES

Example 1:
==========

Consider this example of parsing a crontab, re-schedule a cron event,
and commit the changes. Given:

    30 5 * * 1-5 /bin/foo

we want to re-schedule the event to run at 4:30a instead. Here is one
way, creating an B<Event> object:

    my $ct = new Config::Crontab; $ct->read;
    my ($event) = $ct->select(-command_re => '/bin/foo');
    $event->hour(4);
    $ct->write;

and here is a shortcut to do the same thing:

    my $ct = new Config::Crontab; $ct->read;
    ($ct->select(-command_re => '/bin/foo'))->hour(4);
    $ct->write;

Example 2:
==========

The following example is equivalent to 'crontab -l':

    $ct = new Config::Crontab; $ct->read; print $ct->dump;

Example 3:
==========

The following example is equivalent to 'crontab -u joe -l':

    $ct = new Config::Crontab( -owner => 'joe' );
    $ct->read;
    print $ct->dump;

You can even copy crontabs from one user to another:

    ## must be run as root or other privileged user
    $ct = new Config::Crontab( -owner => 'joe' );
    $ct->read;
    $ct->owner('sherman');
    $ct->write;

Example 4:
==========

You may also make a backup of your crontab:

    $ct = new Config::Crontab; $ct->read;
    $ct->file('mycrontab.bak'); $ct->write;

You can also disable an entire block of commands. Consider the
following crontab file:

    # refresh apache duds
    1 0 * * * kill -1 `ps -x | egrep 'Master:.+? \(https?d\)' | awk '{print $1}'`

    # in-house stats files
    0 1 * * * $HOME/usr/bin/mkstats2.3/mkstats.pl -c online.config -v
    2 1 * * * $HOME/usr/bin/mkstats2.3/mkstats.pl -c police.config -v
    4 1 * * * $HOME/usr/bin/mkstats2.3/mkstats.pl -c scott.config -v
    6 1 * * * $HOME/usr/bin/mkstats2.3/mkstats.pl -c tubbing.config -v
    8 1 * * * $HOME/usr/bin/mkstats2.3/mkstats.pl -c perlcode.config -v
    10 1 * * * $HOME/usr/bin/mkstats2.3/mkstats.pl -c fritzen.config -v
    12 1 * * * $HOME/usr/bin/mkstats2.3/mkstats.pl -c josh.config -v
    14 1 * * * $HOME/usr/bin/mkstats2.3/mkstats.pl -c rhett.config -v

    # do log files
    0 3 * * * $HOME/usr/bin/movelogs.pl -v -k -a

we want to turn off all the stats processing. So we do this:

    $ct = new Config::Crontab; $ct->read;
    $ct->block($ct->select(-command_re => 'mkstats'))->active(0);
    $ct->write;

and here's another way:

    $ct = new Config::Crontab; $ct->read;
    $_->active(0) for $ct->select(-command_re => 'mkstats');
    $ct->write;

Notice all the mkstats lines are commented out:

    # refresh apache duds
    1 0 * * * kill -1 `ps -x | egrep 'Master:.+? \(https?d\)' | awk '{print $1}'`

    # in-house stats files
    #0 1 * * * $HOME/usr/bin/mkstats2.3/mkstats.pl -c online.config -v
    #2 1 * * * $HOME/usr/bin/mkstats2.3/mkstats.pl -c police.config -v
    #4 1 * * * $HOME/usr/bin/mkstats2.3/mkstats.pl -c scott.config -v
    #6 1 * * * $HOME/usr/bin/mkstats2.3/mkstats.pl -c tubbing.config -v
    #8 1 * * * $HOME/usr/bin/mkstats2.3/mkstats.pl -c perlcode.config -v
    #10 1 * * * $HOME/usr/bin/mkstats2.3/mkstats.pl -c fritzen.config -v
    #12 1 * * * $HOME/usr/bin/mkstats2.3/mkstats.pl -c josh.config -v
    #14 1 * * * $HOME/usr/bin/mkstats2.3/mkstats.pl -c rhett.config -v

    # do log files
    0 3 * * * $HOME/usr/bin/movelogs.pl -v -k -a

Re-activating these lines is a matter of setting active to 'true':

    $ct = new Config::Crontab; $ct->read;
    $ct->block($ct->select(-command_re => 'mkstats'))->active(1);
    $ct->write;

INSTALLATION



( run in 0.483 second using v1.01-cache-2.11-cpan-df04353d9ac )