Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Getting Config::Crontab to Write File to Crontab Command

by dperriero (Novice)
on Jul 01, 2005 at 15:34 UTC ( #471755=perlquestion: print w/replies, xml ) Need Help??
dperriero has asked for the wisdom of the Perl Monks concerning the following question:

This is where I am at...I've been able to write a file with the correct syntax, but how do I actually import that file into crontab itself. i.e. crontab -u <user> <file> Here is what I have done so far...
my $ct = new Config::Crontab; my @event; if ($special eq 'hourly'){ @event = ( new Config::Crontab::Comment(-data => '## foo' ), new Config::Crontab::Event( -datetime => '4 * * * Sun', -command => "/usr/bin/perl + $bsql::cron_command")); } elsif ($special eq 'daily'){ @event = ( new Config::Crontab::Comment(-data => '## foo' ), new Config::Crontab::Event( -minute => 00, -hour => 4, -command => "/usr/bin/perl +$bsql::cron_command")); } elsif ($special eq 'weekly'){ @event = ( new Config::Crontab::Comment(-data => '## foo' ), new Config::Crontab::Event( -dow => 'Sun', -minute => 00, -hour => 4, -command => "/usr/bin/perl +$bsql::cron_command")); } elsif ($special eq 'monthly'){ @event = ( new Config::Crontab::Comment(-data => '## foo' ), new Config::Crontab::Event( -datetime => '0 0 4 1 * *', -command => "/usr/bin/perl + $bsql::cron_command")); } my $block = new Config::Crontab::Block; ## add this block to crontab file $block->lines(\@event); $ct->last($block); ## write out crontab file $ct->write("$bsql::dir/backup/crontab");

Replies are listed 'Best First'.
Re: Getting Config::Crontab to Write File to Crontab Command
by waswas-fng (Curate) on Jul 01, 2005 at 15:59 UTC
    Config::Crontab's POD does have a long example for how to read/write users crontabs in it...
    $c = new Config::Crontab; $c->owner('joe'); $c->read; ## reading joe's crontab $c->write; ##write out to joe's crontab... $c->owner('bob'); #change user to bob $c->write; #write same crontab to bob's crontab


    Update:

    It does look as though Config::Crontab handles restarting crontab and setting other user's crontabs via the system crontab command -- but looking though the code a few things you need to be aware of:

    1: As you can see below the crontab command is not absolute -- you should make sure your PATH is set correctly before calling this write function -- or better yet modify Config::Crontab to use a setting to define where crontab exists on the filesystem (and maybe submit a patch).
    my $crontab; if( my $owner = $self->owner ) { $crontab = `crontab -u $owner $tmpfile 2>&1`; } else { $crontab = `crontab $tmpfile 2>&1`; } chomp $crontab; unlink $tmpfile; if( $crontab || $? ) { $self->error($crontab); if( $self->strict ) { carp "Error writing crontab (crontab exited with status " . ($? >> 8) . "): " . $self->error; } return; } } return 1; }
    2: The Module does not check if you are root before trying to update any users crontab -- if you are modding users crontabs other than your running UID, you should run as root and also verify the script is running as root before you call the writes.


    -Waswas

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://471755]
Approved by Limbic~Region
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2018-08-17 05:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:









    Results (174 votes). Check out past polls.

    Notices?