Crontab deleted?
crontab
with no arguments reads a crontab file from standard input. For example, you might use:
echo "* * * * * run-this-every-minute" | crontab
Once you've clobbered your crontab (i.e., crontab -l
shows nothing), there's no good way to get it back.
On my system (Ubuntu 11.04), personal crontabs are stored in /var/spool/cron/crontabs/<USER>
-- but that's what you clobbered, so that won't do you any good. (The path could be different on your system.)
I see entries in /var/log/syslog
for commands executed by cron
; you might be able to reconstruct your crontab from that (or your system's equivalent, if any), but it's going to be tedious.
Here's what I do to avoid this kind of problem:
I keep my crontab in a separate file, maintained in a source control system. I install it only by running
crontab filename
I never use crontab -e
. If I accidentally clobber my crontab, I can just reload it from the file. (Well, hardly ever; I sometimes use crontab -e
to make temporary changes, knowing that I can restore the current version later.)
Script for full crontab recovery
I made a PHP script that does a full recovery of your crontab, based on the log.
It outputs a single instance of every cron command run by the user for the last week.
I put it here
https://github.com/dangreenisrael/recover_crontab
Here is a sample output:
perl ~/sorttv/sorttv.pl
/usr/local/bin/flexget
bash ~/scripts/sort_sports.sh
~/scripts/play_recently_added.sh