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.
May 11th, 2005 at 9:38 am
You silly man deleting your crontab file.
May 11th, 2005 at 2:22 pm
lol - i think he made this just for me :-p
August 6th, 2005 at 7:49 am
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?
August 21st, 2006 at 10:23 pm
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.
September 26th, 2006 at 4:44 pm
Hi. I’ve just made crontab -r :(( damn
and google found this page, thx for script
March 6th, 2007 at 5:11 pm
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 :))
June 14th, 2007 at 10:21 am
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.