#!/usr/local/bin/perl # @(#) edcron Edit crontab file # Usage: # -v be verbose # -u user edit user crontab file # pat@jaameri.gsfc.nasa.gov (patrick m. ryan) require 'getopts.pl'; &Getopts('vu:'); $tmpdir = $ENV{TMPDIR} || "/tmp"; $tmp = $tmpdir . "/.crontab.$<.$$"; $edit = $ENV{VISUAL} || $ENV{EDITOR} || "vi"; $user = $opt_u || ''; # put crontab file into a temporary file $cmd = "crontab -l $user > $tmp"; if ($opt_v) { print $cmd,"\n"; } system $cmd; chmod(0600,$tmp); if ($?>>8) { unlink $tmp; die "could not execute \"$cmd\""; } # edit the temporary crontab file $mtime = (stat $tmp)[9]; $cmd = "$edit $tmp"; if ($opt_v) { print $cmd,"\n"; } system $cmd; if ($?>>8) { unlink $tmp; die "could not execute \"$cmd\""; } unless ((stat $tmp)[9] > $mtime) { print "no change\n"; exit 0; } print "Install modified crontab file? (y/n) "; $resp = ; $resp =~ s/^\s*//; unless ($resp eq "" || $resp =~ /^y/i) { print "No change\n"; exit 0; } $cmd = "crontab $tmp"; if ($opt_v) { print $cmd,"\n"; } system $cmd; if ($?>>8) { unlink $tmp; die "could not execute \"$cmd\""; } unlink $tmp; exit 0;