crontab -r

I understand the idea behind using ‘e’ (edit) and ‘r’ (remove) for crontab. But the problem with that is the ‘e’ is dangerously close to the ‘r’. After accidentally deleting my cron file for the 80th time, I put together this quick Perl script that adds two features to crontab.

The first feature is to prompt the user when the remove option is used and the second option is –force, which when combined with -r mimics the old crontab operation.

Maybe I just have clumsy fingers, but this script has saved me multiple times and eased my worry over accidentally deleting the crontab.

The use this script, do the following -
1) Create /usr/local/scripts/safe_crontab.pl with the following text:

#!/usr/bin/perl -w
use strict;

my $run_crontab = 1;
my @safe_argv;
my ( $bForce, $bRemove, $bHelp );

foreach ( @ARGV ) {
    if ( uc( $_ ) eq '--FORCE' ) {
        $bForce = 1;
    } else {
        if ( uc( $_ ) eq '-R' ) {
            $bRemove = 1;
        }
        push( @safe_argv, $_ );
    }
}

if ( $bRemove ) {
    unless( $bForce ) {
        my ( $y_or_n );
        do {
            print 'Are you sure you want to remove the crontab? (Y/N): ';
            $y_or_n = uc( getc() );
            print "\n";
        } until ( ( $y_or_n eq 'Y' ) || ( $y_or_n  eq 'N' ) );
        if ( $y_or_n eq 'N' ) {
            print "/usr/bin/crontab: no changes made to crontab\n";
            $run_crontab = 0;
        }
    }
}

if ( $run_crontab ) {
    if ( system( '/usr/bin/crontab', @safe_argv ) == 256 ) {
        print "\t--force (delete crontab without prompting)\n";
    }
}

2) Add this line to /etc/bashrc
alias crontab='/usr/local/scripts/safe_crontab.pl'

3) Run the following command:
chmod 755 /usr/local/scripts/safe_crontab.pl

4) Log off and back on and you should be all set. Hope you find this useful.

7 Responses to “crontab -r”

  1. 1
    Shiner_Man Says:

    You silly man deleting your crontab file.

  2. 2
    shftleft Says:

    lol - i think he made this just for me :-p

  3. 3
    Andy Says:

    I had a customer who did this twice in successive days and then wondered why his backup wouldn’t work. After restoring from a (now old) backup for the second time I made sure there was a copy of his crontab stored on disk. Maybe an entry of crontab -l >crontab.txt should be added to the crontab?

  4. 4
    Rolfs Says:

    I’m used to crontab -l > cron_or_whateverfilename
    then edit the file;
    then crontab cron ..

    it’s just what you’re used to.. And ofcause, the cron file in home-dir is often left behind, not removed giving a sort of backup.. without the need of a perl script.. but nice work :-) Or as wordpress sais it: “Code is poetry”.

    Btw, some gehsi syntax hilighting would be nice.

  5. 5
    Michael Says:

    Hi. I’ve just made crontab -r :(( damn

    and google found this page, thx for script

  6. 6
    Jimbo Says:

    I work in an environment where essentially the entire company has access to the crontabs (very bad idea!) and yes we’ve had folks accidentally issue crontab -r, which of course really hoses us until we can pull the crontab from a backup.

    Just thought I’d share the pain. :)

    Cool script btw, although our admins would never install it because they love being reactive to problems instead of proactive :))

  7. 7
    hanov3r Says:

    I just ‘-r’ed when I should have ‘-e’ed this morning. Your script looks useful, but why not just alias ‘crontab’ to ‘crontab -i’? ‘man crontab’ tells me that that forces an interactive ‘yes/no’ on crontab removal.

Leave a Reply

Search

Subscribe!

Subscribe to the RSS Feed

Have a question or comment?

Send me an email - mincus@gmail.com

Most Popular

Recent Posts

Categories

Sponsors

Links

Meta

Classic Kids Games