72

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?

  • 39
    Is it just me or wouldn't it make more sense for crontab -r to ask yes/no by default?? – user1446688 Feb 16 '15 at 22:54
  • 7
    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 '15 at 15:52
  • 5
    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 '15 at 13:35
  • 8
    This has happened to me twice already. It's the worst idea ever to have e for edit and r for remove with absolutely no prompt whatsoever!! – DaniG2k Sep 7 '15 at 14:17
  • 10
    First thing to do, alias crontab=crontab -i. But crontab should have made that default, given that e & r are next to each other... – anishsane Dec 8 '15 at 6:57
56

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.
  • On Ubuntu/Debian, if your task has run before, try grep CRON /var/log/syslog
| improve this answer | |
  • I could not voted you up but this answer was some how useful for me. thankx – Teerath Kumar 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! – hellodanylo 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 '15 at 15:49
  • +1 for being straightforward :} – namezero Apr 13 '18 at 17:54
  • You can try your luck looking for a backup made by the editor in /tmp/crontab.wpMhKS (of course the name may change). It may not be the most recent version but in my case it helped a lot – Jong Bor Aug 3 '18 at 14:11
23

If you have no /var/log/cron file you can recover the commands (but not the timings) from the syslog.

grep 'CRON.*(yourusername)' /var/log/syslog

you can then figure out most timings by looking at the datestamps.

| improve this answer | |
  • 1
    On ubuntu the file is /var/log/syslog. – Aurelijus Rozenas Nov 29 '16 at 13:02
  • @ Aurelijus Rozenas I accidentally added sys to the directory path it should be /var/log/syslog, I corrected my post. – Meow Nov 29 '16 at 17:32
  • 2
    grep 'CRON.*(yourusername)' /var/log/syslog – aidan Dec 15 '16 at 22:52
  • @Aidin you are my savior! Note to self: Never edit the crontab file while server space is full. – Shadoath Feb 6 '17 at 17:55
  • 1
    Phew that was close! I've now added a cron to backup my crontab everyday :) – SidJ Jul 5 '19 at 6:20
0

It sucks, but if you run crontab -r your crontab is gone forever. And unless you have a backup of it somewhere, you must grep through syslog files to get an idea of what/when jobs were being run, and recreate.

A good trick to avoid such problem is to add the following line to your crontab:

@daily          crontab -l > $HOME/.crontab

This way your crontab is backed up every day to $HOME/.crontab, so if you delete it by accident, a relatively recent copy is available and can be installed by:

crontab < $HOME/.crontab
| improve this answer | |
-6

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

| improve this answer | |
  • 4
    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 '15 at 13:06
  • Upvote from me for actually stating the location of the crontab. I had an entire system backup and could easily retrieve my cronjobs. Thanks! – Cookie Dec 18 '15 at 14:24

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