I’ve never personally had this problem, but a number of people have told me that they’ve, often repeatedly, accidentally deleted their crontab by typing
crontab -r (which silently removes a crontab) rather than
crontab -l (which shows you what is in it) or
crontab -e (which lets you edit it). It doesn’t help that “e” and “r” are next to each other on QWERTY keyboards.
Create a single backup of your crontab contents
Since I realised this was an issue, I’ve made the first line in my crontabs the following:
@daily crontab -l > ~/crontab.backup
If you ever accidentally use
crontab -r, you can use
crontab ~/crontab.backup to reinstall your crontab!
Adjust @daily to a time at which your computer is likely to be on, if it’s not always on, eg
0 10 * * * for 10am daily.
For bonus points, writing this entry reminded me that I hadn’t reinstalled my laptop’s crontab on my new machine, and meant it was easy for me to find and install!
Create timestamped backups of your crontab contents
The above is simple and suffices for me, but if you don’t have a backup routine that will grab
~/crontab.backup regularly enough for your needs, you could do something like this instead:
@daily mkdir -p ~/crontab-backups; crontab -l > ~/crontab-backups/crontab-`date +%Y%m%d-%H%M%S`; find ~/crontab-backups -type f -ctime +7 -delete
mkdir -p ~/crontab-backupsmakes a directory
crontab-backupsin your home directory if it doesn’t already exist (and doesn’t complain if it does exist).
crontab -l > ~/crontab-backups/crontab-`date +%Y%m%d-%H%M%S`puts your current crontab into a file named with a datestamp (eg
crontab-20140711-124450so that you can easily have more than one
find ~/crontab-backups -type f -ctime +7 -deletefinds all files (
-type f) in
~/crontab-backupsthat were created more than 7 days ago (
-ctime +7) and deletes them (
Warning: you don’t want to put anything else in
~/crontab-backups, because it too will be deleted after seven days.
One Reply to “Your crontab file should start with “crontab -l”!”
@puzzlement @Skud Better: Don’t use crontab(1) at all, ever. Use /etc/cron.d and friends. (And/or: Include /var/spool/cron in backups.)
Comments are closed.