After having my crontab deleted for the 3rd time from "fat fingering" -r instead of -e(they're so close!), I created this little script that adds a prompt to delete the crontab.
1) Create /usr/local/scripts/safe_crontab.pl with the code below.
2) Add this line to /etc/bashrc (or your personal .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.
#!/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\";
}
}