Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I entered crontab -r instead of crontab -e and all my cron jobs have been removed.

What is the best way (or is there one) to recover those jobs?

share|improve this question
    
Is it just me or wouldn't it make more sense for crontab -r to ask yes/no by default?? –  user1446688 Feb 16 at 22:54
    
I think having a yes/no prompt would be a great idea. especially since e & r are right next to each other.. and crontab -e is a really common cron command. –  JustinP May 11 at 15:52
    
To futureproof, instead of crontab -e consider using a variation of this process (assuming $HOME directory): crontab -l >.crontab ; vi .crontab ; sleep 2 && crontab .crontab and thereafter vi .crontab ; sleep 2 && crontab .crontab. I have an extension to vi that returns status indicating whether or not the file contents changed during the edit. I can then ifvi .crontab && crontab .crontab. (But that extension is out of scope for a comment.) –  roaima Jun 23 at 13:10
    
In 7 or so years of admin work I have never had this happen to me. Now I am scared. Time to start backing up the crontabs regularly. –  Caja Jun 23 at 13:35

2 Answers 2

up vote 9 down vote accepted

crontab -r removes the only file containing the cron jobs.

So if you did not make a backup, your only recovery options are:

  • On RedHat/CentOS, if your jobs have been triggered before, you can find the cron log in /var/log/cron. The file will help you rewrite the jobs again.
  • Another option is to recover the file using a file recovery tool. This is less likely to be successful though, since the system partition is usually a busy one and corresponding sectors probably have already been overwritten.
share|improve this answer
    
I could not voted you up but this answer was some how useful for me. thankx –  RajaRassani Jun 10 '14 at 8:24
1  
@RajaRassani You can mark the answer is a correct one with a tick mark, though. Thanks for feed back! –  SkyDan Jun 10 '14 at 8:31
    
this just happened to me :) total bummer.. I do use virtual servers and backup them up regularly..so I'm going to spin up a backup.. and get the deleted crontabs –  JustinP May 11 at 15:49

vi /var/spool/cron/*user* or if you're the root user then vi /var/spool/cron/root

share|improve this answer
2  
This is worse. It edits the spool files without notifying cron that the file has been edited. It also won't work when the user has accidentally deleted their crontab because there is no file to edit. –  roaima Jun 23 at 13:06

protected by Michael Mrozek Jun 23 at 16:36

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.