Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Adding into a users crontab

by ninjaspydr (Initiate)
on Jan 09, 2007 at 18:03 UTC ( #593763=perlquestion: print w/replies, xml ) Need Help??

ninjaspydr has asked for the wisdom of the Perl Monks concerning the following question:

Would I use the module Schedule::Cron to access a users crontab? I am writing a script that I want run about every 5 minutes, but I want it set like the user used the command crontab -e.
Am I going about this right?

Replies are listed 'Best First'.
Re: Adding into a users crontab
by liverpole (Monsignor) on Jan 09, 2007 at 19:17 UTC
    Hi ninjaspydr,

    No, Schedule::Cron is used to create a "cron-like" scheduler.  It doesn't make use of the actual cron daemon.

    If you want to go through the normal cron mechanism, just create create the file you want to add to the crontab, and insert it with crontab <file>.

    You can get more information about it from the man page for crontab:  man crontab.

    Naturally, you can automate the process quite easily with Perl.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Adding into a users crontab
by kyle (Abbot) on Jan 09, 2007 at 19:27 UTC

    Are you trying to modify an existing crontab to run your script, or are you trying to get your script to act as if it is the scheduler?

    If you want to modify the user's crontab, you might be able to do something like this:

    open my $ct_in, '-|', 'crontab -l' or die "Can't read crontab: $!"; my $crontab_contents = do { undef $/; <$ct_in> }; close $ct_in or die "Can't close crontab after read: $!"; open my $ct_out, '|-' 'crontab -' or die "Can't write new crontab: $!" print $ct_out $crontab_contents; print $ct_out "\n"; print $ct_out "*/5 * * * * /every/five/minutes\n"; close $ct_out or die "Can't close crontab after write: $!";

    You can get the format of the crontab file from man 5 crontab.

Re: Adding into a users crontab
by BaldPenguin (Friar) on Jan 09, 2007 at 21:44 UTC
    Maybe you could use Config::Crontab

    It looks like it would work, but could be some overhead as opposed to the above proposal of simple read and write.

    Don
    WHITEPAGES.COM | INC
    Everything I've learned in life can be summed up in a small perl script!
      BaldPenguin, Thanks a bunch. That did the trick. Cheers.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2021-11-29 20:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?