2

How could I append a cron task in crontab via a perl script?
I thought of the following:

#!/usr/bin/perl  

use strict;  
use warnings;  

`crontab<<EOL  
00 * * * * /home/slynux/download.sh    
EOL`  

I don't want to mess up things, so am I on the right track?
Also if I append it, how would I remove it? I am new in Perl

Cratylus
  • 52,998
  • 69
  • 209
  • 339
  • Stack overflow is a place for asking specific questions about programming problems you are having. Please post the code you've written, what it is doing, and the problem you are having. Please provide any relevant source material as well. – xaxxon Aug 08 '13 at 18:39
  • @xaxxon:The way you have put it I might as well delete the OP completely. I have not written any code, I am looking for a way to go.See this relevant thread http://stackoverflow.com/questions/18132144/handle-race-condition-between-2-cron-tasks-what-is-the-best-approach?noredirect=1#comment26553722_18132144 – Cratylus Aug 08 '13 at 18:41
  • This seems like a bad idea. If you want to schedule a job to run once or twice at a specific time, look into `at(1)` instead of editing and then unediting the crontab. – mob Aug 08 '13 at 18:41
  • @mob:`at(1)`? What do you mean? – Cratylus Aug 08 '13 at 18:42
  • @Cratylus you posted some code. does that do what you want? if not, what is wrong with it? – xaxxon Aug 08 '13 at 18:43
  • 1
    The Unix command called `at`. Read the documentation by running `man at` or `man 1 at`. – mob Aug 08 '13 at 18:43
  • @xaxxon: I am looking for how would I "undo" this command, if I follow this way – Cratylus Aug 08 '13 at 18:44
  • I keep my crontab in a file (under source control), and use `crontab filename` to install it. That guarantees that I can keep track of what changes I've made, and roll back to an earlier crontab if I mess things up. It's hard to see what a Perl script would add to this process. You can certainly use a Perl script to update the file and/or to invoke the `crontab` command; personally, I use an editor and a shell for that. – Keith Thompson Aug 08 '13 at 18:50
  • @mob:+1 interesting!So `at` is a one shot scheduling? Could I use `at` to make script reschedule itself via `at`? – Cratylus Aug 08 '13 at 18:51
  • @KeithThompson:It is only to avoid the manual modification of crontab and let the script finish their jobs unattended – Cratylus Aug 08 '13 at 18:52
  • @mob:Could you please check this if you spare some time http://stackoverflow.com/questions/18132144/handle-race-condition-between-2-cron-tasks-what-is-the-best-approach?noredirect=1#comment26553722_18132144 ? – Cratylus Aug 08 '13 at 18:53
  • @Cratylus: You still have to write the script, presumably manually. It's not at all clear what value you're trying to add to the existing process of using the `crontab` command. I'm not saying that what you're trying to do isn't useful, just that you haven't explained it. – Keith Thompson Aug 08 '13 at 18:56
  • @mob:Seems that `at` is not available by default – Cratylus Aug 08 '13 at 18:58
  • @KeithThompson:I will write it once to solve a recurrent problem – Cratylus Aug 08 '13 at 18:58
  • @KeithThompson:Check this for the why:http://stackoverflow.com/questions/18132144/handle-race-condition-between-2-cron-tasks-what-is-the-best-approach?noredirect=1#comment26553722_18132144 – Cratylus Aug 08 '13 at 19:07
  • Apropos daily WTF about a situation where you probably should have used `at`: http://thedailywtf.com/Articles/A-Crony-Joke.aspx – mob Aug 09 '13 at 16:13
  • Why is this an off-topic???This is a programming question – Cratylus Aug 10 '13 at 08:19

2 Answers2

6

Quick & dirty way :

#!/usr/bin/perl  

use strict; use warnings;  

`(crontab -l; echo "00 * * * * /home/slynux/download.sh") | crontab -`;

Another (better) approach :

#!/usr/bin/perl  

use strict; use warnings;  

open my $fh, "| crontab -" || die "can't open crontab: $!";
my $cron = qx(crontab -l);
print $fh "$cron\n0 * * * * /home/slynux/download.sh\n";
close $fh;

To remove the crontab line(s) with /home/slynux/download.sh :

#!/usr/bin/perl  

use strict; use warnings;

open my $fh, "| crontab -" || die "can't open crontab: $!";
my $cron = qx(crontab -l);
$cron =~ s!.*/home/slynux/download\.sh.*!!g;
print $fh $cron;
close $fh;
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
0

A quick search on metacpan returns Config::Crontab. While I have never used this module, it looks like it would do what you want.

mirod
  • 15,923
  • 3
  • 45
  • 65